9

Just wondering the best way (even if via Portal, Powershell, or C#) to purge the messages off of a Service Bus Topic's Subscription.

Imagine we have a topic with 4 subscriptions, and we only want to purge the messages from one of the subscriptions.

I have a feeling the only way may be to read the messages in a while loop, but hoping for something better.

UPDATE:

Apart from using code, you can use the Server Explorer as suggested in the answer - right click subscription and purge messages:

enter image description here

Stefan Zvonar
  • 3,959
  • 3
  • 24
  • 30
  • Using SBExplorer, you can also use "Receive Messages" with "Receive and Delete" option. There's a tracking issue to support this issue server side: https://github.com/Azure/azure-service-bus/issues/1 – Sean Feldman Oct 25 '17 at 03:13

3 Answers3

9

You can most certainly do it via code. If you're using Service Bus SDK, you could do something like the following:

    static void PurgeMessagesFromSubscription()
    {
        var connectionString = "Endpoint=sb://account-name.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=access key";
        var topic = "topic-name";
        var subscription = "subscription-name";
        int batchSize = 100;
        var subscriptionClient = SubscriptionClient.CreateFromConnectionString(connectionString, topic, subscription, ReceiveMode.ReceiveAndDelete);
        do
        {
            var messages = subscriptionClient.ReceiveBatch(batchSize);
            if (messages.Count() == 0)
            {
                break;
            }
        }
        while (true);
    }

What this code will do is fetch messages from the subscription (100 at a time) in Receive & Delete mode so that as soon as messages are fetched, they are deleted from the subscription automatically.

I believe Service Bus Explorer tool also has the capability to purge messages. You can use that as well instead of writing the code.

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
3

If you have a lot of messages and can tolerate a bit of downtime on subscriber side, it might be faster to just drop the subscription and create a new one with the same name.

Mikhail Shilkov
  • 34,128
  • 3
  • 68
  • 107
  • This is interesting! Do you know how much time does Azure Service Bus takes to delete a subscription? From my experience with Storage Queues, it may take up to 30 seconds or more for a queue to get actually deleted before which you can create a new queue with the same name. – Gaurav Mantri Oct 25 '17 at 06:49
  • I don't have any formal timings, but I did it several times and it seemed pretty much instantaneous. – Mikhail Shilkov Oct 25 '17 at 07:10
  • How do you do so through code in a ASP.NET Core application? – droft1312 Nov 26 '19 at 15:23
0

Thank you @Gaurav Mantri, I used slightly changed code without the batch option with version 5.2.0 of Microsoft.Azure.ServiceBus Nuget Package:

var connectionString = "Endpoint=sb://";
var topic = "topic";
var subscription = "subscription";
var subscriptionClient = new SubscriptionClient(connectionString, topic, subscription, ReceiveMode.ReceiveAndDelete);

subscriptionClient.RegisterMessageHandler(
  (message, token) =>
    {
     Console.WriteLine($"Received message: SequenceNumber: 
             {message.SystemProperties.SequenceNumber}");

      return Task.CompletedTask;
     },
   (exceptionEvent) =>
    {
     Console.WriteLine("Exception = " + exceptionEvent.Exception);
     return Task.CompletedTask;
    });
Anand Sowmithiran
  • 2,591
  • 2
  • 10
  • 22
eyesfree
  • 126
  • 2
  • 5