Ever wondered on how you can remove the scheduled message in Service bus topic or queue? We had a bug where one of our service keep queuing the messages while its not meant to be queued and we deployed the fix but in order to test the fix we need to make sure there are no messages are being scheduled therefore we need to remove all scheduled messages to test if the fix is working

At the same time you can also use the same code to check your messages if it is all being scheduled properly based on your logic

I was expecting the service bus explorer on azure portal allow us to peek into this scheduled messages but unfortunately it doesn’t have this feature

for service bus topic you can use below code

class Program
    {

        // Connection String for the namespace can be obtained from the Azure portal under the
        // 'Shared Access policies' section.
        const string ServiceBusConnectionString = "[Servicebus connection string with entity path]";
        static ITopicClient topicClient;
        static IMessageReceiver messageReceiver;

        static void Main(string[] args)

        {
            MainAsync().GetAwaiter().GetResult();
        }

        static async Task MainAsync()
        {
            var sbConnStringBuilder = new ServiceBusConnectionStringBuilder(ServiceBusConnectionString);
            topicClient = new TopicClient(sbConnStringBuilder);
            Console.WriteLine("======================================================");
            Console.WriteLine("Press any key to exit..");
            Console.WriteLine("======================================================");

            messageReceiver = new MessageReceiver(sbConnStringBuilder);

            Message message = await messageReceiver.PeekAsync();

            // Message with property ScheduledEnqueueTimeUtc not null means it is a scheduled message

            while (message != null)
            {
                if (message != null && message.ScheduledEnqueueTimeUtc != null)
                {
                    // Remove the scheduled message
                    await topicClient.CancelScheduledMessageAsync(message.SystemProperties.SequenceNumber);
                }
                message = await messageReceiver.PeekAsync();
            }

            Console.ReadKey();
            await topicClient.CloseAsync();
        }

    }

For the service bus queue you can use code below

class Program
    {

        // Connection String for the namespace can be obtained from the Azure portal under the
        // 'Shared Access policies' section.
        const string ServiceBusConnectionString = "[Servicebus connection string with entity path]";
        static IQueueClient queueClient;
        static IMessageReceiver messageReceiver;

        static void Main(string[] args)

        {
            MainAsync().GetAwaiter().GetResult();
        }

        static async Task MainAsync()
        {
            var sbConnStringBuilder = new ServiceBusConnectionStringBuilder(ServiceBusConnectionString);
            queueClient = new QueueClient(sbConnStringBuilder);
            Console.WriteLine("======================================================");
            Console.WriteLine("Press any key to exit..");
            Console.WriteLine("======================================================");

            messageReceiver = new MessageReceiver(sbConnStringBuilder);

            Message message = await messageReceiver.PeekAsync();

            // Message with property ScheduledEnqueueTimeUtc not null means it is a scheduled message

            while (message != null)
            {
                if (message != null && message.ScheduledEnqueueTimeUtc != null)
                {
                    // Remove the scheduled message
                    await queueClient.CancelScheduledMessageAsync(message.SystemProperties.SequenceNumber);
                }
                message = await messageReceiver.PeekAsync();
            }

            Console.ReadKey();
            await queueClient.CloseAsync();
        }