title: Azure Service Bus - Delete Specific Message
body: <p>I'm developing a system which will involve a lot of data synchronisation broken down into small tasks. I am adding each small task as a job/message on the Azure Service Bus queue.</p>
  
  <p>I have X number of Worker Roles then checking the queues and processing the data.</p>
  
  <p>I don't expect to have many messages in the queue because the aim is to process a message, complete it and then re-add the same message again, but scheduled for X minutes time. This will give me a loop to continue to process those tasks.</p>
  
  <p>The great thing about the Azure functionality is that they handle all of the server side stuff for you, but the downside is that it can sometimes be difficult to debug or manipulate the data.</p>
  
  <p>What I want to be able to do is present a list of messages in the queue (Which I have done using PeekBatch) in a web interface. I then want to be able to select some/all of the messages and delete them.</p>
  
  <p>I might want to do this if there is a bug in the code and I want to stop messages of a certain type to stop.</p>
  
  <p>Following on from that I'll have functionality to re-add messages from the web page too. Perhaps I might want to up my worker roles and messages to perform a task at a faster rate (or slow them down), or re-add messages I have deleted.</p>
  
  <p>So, the question is, how can I actually select a specific message from the queue and then delete it? From what I can see, there is no obvious way to do this and, if it's possible, it will require some kind of workaround. This sounds a bit bizarre to me.</p>
  
  <p>Edit:</p>
  
  <p>I've got something that works, but it really doesn't seem a great solution:</p>
  
  <pre><code>    public void DeleteMessages(List&lt;long&gt; messageIds)
      {
          foreach (var msg in Client.ReceiveBatch(100))
          {
              if (messageIds.Contains(msg.SequenceNumber))
                  msg.Complete(); // Deletes the message
              else
                  msg.Abandon(); // Puts it back in the queue
          }
      }
  </code></pre>
  
  <p>This will get less and less efficient the larger the queue, but it at least does stop all activities whilst the delete call is going on and deletes the specified messages.</p>
  
  <p>It will also only delete messages which are ready to process. Messages in the future will be ignored, so I've currently added the ability to add "Sleep" messages, to stop processing the queue until my messages are "ready" and I can delete them. </p>
  
  <p>I've been informed by Microsoft that they are currently working on the API to delete specific messages which should be available in a few months. Until then, it's all about workarounds.</p>
  
  <p><strong>June Update:</strong></p>
  
  <p><strong>Still</strong> no update from Microsoft on this issue and the above method was less than ideal. I have now modified my code so that:</p>
  
  <p>The object I put into the message has a new property: </p>
  
  <pre><code>Guid? MessageId { get; set; }
  </code></pre>
  
  <p><em>Note it is nullable Guid just to be backwards-compatible</em> </p>
  
  <p>When I want to delete a message, I add my MessageId into a database table "DeletedMessage".</p>
  
  <p>When it comes to processing a message, I look in the DeletedMessage table for a matching Guid and if it finds one I simply Complete() the message without doing the normal processing.</p>
  
  <p>This works well, but is a slight overhead. If you are not dealing with huge numbers of messages it's not a big problem.</p>
  
  <p>Also note that I did this originally by using the SequenceNumber, but (bizarrely) the SequenceNumber changes between peeking and retrieving the message! This prevents the idea from working, unless you use your own Id as above.</p>