Clearing the Dead Letter Queue on an Azure Service Bus Queue
Whilst testing out the Azure Function I had written to listen for incoming messages to an Azure Service Bus queue, I ended up with quite a few messages landing in the Dead Letter Queue. A combination of messing around whilst trying to get something to work.
Just to be clear, I am using an Azure Service Bus queue, not to its sibling the topic / subscription. My task was to figure out how to remove these messages. Even though it was a test queue that I was playing around with, I thought it would still be a useful exercise to learn how to clear messages from the Dead Letter Queue as this is something I would almost certainly need to know once we went live with the production queue.
As with many tasks relating to the Microsoft stack, there seemed to be many different ways of achieving this, and it was getting confusing trying to find which one was applicable to my particular circumstances.
After some trial and error I eventually managed to get the following code to work. Basically you connect to your Dead Letter Queue in exactly the same way as your normal queue, but you need to contatenate “$DeadLetterQueue” to the queue name.
Hide Copy Code
public void ClearDeadLetterQueue()
string deadLetterQueueName = "myQueue/$DeadLetterQueue";
QueueClient client = QueueClient.CreateFromConnectionString("connectionString",
while (client.Receive() != null)
var receivedMessage = client.Receive();
//do something with the message here
After running the following code in my unit test I successfully managed to clear all the messages from my Dead Letter Queue.