Cleaning up deferred messages on Azure Service Bus without the sequence number

azure, azureservicebus

Solution

Peeking messages from a queue or subscription will also return deferred messages. Deferred messages will have State = "Deferred".

This way you can get the sequence numbers of the deferred messages and afterwards process or delete these messages.

You can try this out in ServiceBus Explorer:

Problem

Is there any way to recover or delete a deferred message on Azure Service Bus if I've lost the sequence number? The scenario is: I want to use `BrokeredMessage.Defer()` to defer a message. I plan to record the sequence number, and use it later on to retrieve the message. But if something goes wrong — let's say some buggy code is deployed — and the sequence number is not recorded properly, it seems like that message will sit on the service bus in a deferred state until the message expires, which could be forever. This concerns me primarily because that message will occupy space on the queue or subscription, and I haven't found any way of recovering that space short of completely deleting the queue/subscription. Is there any way to either receive or delete "lost" deferred messages?

Original source