Loading...

Purge Deferred Messages in Service Bus

Purge Deferred Messages in Service Bus

What are Deferred messages:

Deferred messages refer to messages that a queue or subscription client is unable to process at the moment due to certain circumstances. Instead of processing it immediately, the client can defer the retrieval of the message to a later time, while the message remains in the queue or subscription.

 

Message Deferral | Azure Service Bus

 

Unlike dead-letter messages that are stored in a subqueue, deferred messages are kept in the main queue along with other active messages. However, these messages cannot be received using regular receive operations. If an application loses track of a deferred message, it can be discovered by browsing through the messages.

 

The responsibility of retrieving a deferred message lies with its owner, who must remember the sequence number as it is deferred. A receiver can later retrieve the deferred message by using receive methods that require the sequence number as a parameter. For further details about sequence numbers, please refer to Message sequencing and timestamps.

 

However, it can be very difficult or even unfeasible to get each sequence number from the queue/subscription when the entity contains thousands of messages.

 

Here is an example of how you can receive or purge all the deferred messages in the entity.

 

Pre-requisites:

  • Service Bus namespace

  • Already created queue/subscription

  • Service Bus Explorer

 

Using Service Bus Explorer:

  1. Download the “Service Bus Explorer” from:  https://github.com/paolosalvatori/ServiceBusExplorer

  2. Open service bus explorer and click File and connect it.

         

Mohsin1400_7-1681810258488.png

 

    3. From the drop down, select connection string and provide the connection string of the namespace level.

        

Mohsin1400_8-1681810258492.png

 

    4. Once it is successfully connected, you will see Service Bus Explorer shows the count of Active messages as shown below.

 

Mohsin1400_9-1681810258493.png

 

Mohsin1400_13-1681810429814.png

 

 

    5. When we peek through the messages using Service bus explorer we can see the status of the messages as Deferred.

 

Mohsin1400_14-1681810447929.png

 

   6. When you click on Purge messages, you will notice that the application keeps loading and the messages are not purged.

 

Mohsin1400_15-1681810472068.png

 

 

Receive/Delete messages using C# Code:

 

Run the below code which will receive and complete all the messages from the mentioned queue/subscription after changing the status of deferred to active.

 

 

using Azure.Messaging.ServiceBus; using Microsoft.Azure.Amqp; class Program { static void Main(string[] args) { receiveDeferredMessages(); } public static async Task receiveDeferredMessages() { List<long> sequencenumbers; string connectionString = "SAS Key"; string queueName = "QueueName"; try { bool condition = true; sequencenumbers = new List<long>(); await using var client = new ServiceBusClient(connectionString); ServiceBusReceiver receiver = client.CreateReceiver(queueName); while (condition) { ServiceBusReceivedMessage peekedMessage = await receiver.PeekMessageAsync(); if (peekedMessage != null && peekedMessage.State.ToString() == "Deferred") { sequencenumbers.Add(peekedMessage.SequenceNumber); } else { condition = false; } } var deferredMessage = await receiver.ReceiveDeferredMessagesAsync(sequencenumbers); foreach (var message in deferredMessage) { await receiver.CompleteMessageAsync(message); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } }

 

 

 

 

 

Published on:

Learn more
Need help with this product?

We can help you with Purge Deferred Messages in Service Bus

If you want help implementing, troubleshooting, or improving this product, contact us and we’ll point you in the right direction.

Azure PaaS Blog articles
Azure PaaS Blog articles

Azure PaaS Blog articles

Share post:

Related posts

Azure Storage - TLS 1.0 and 1.1 retirement

Overview TLS 1.0 and 1.1 retirement on Azure Storage was previously announced for Nov 1st, 2024, and it was postponed recently to 1 year later...

1 year ago

Efficient Management of Append and Page Blobs Using Azure Storage Actions

  Overview In Azure Storage, Blob Lifecycle Management (BLM) allows you to automate the management of your data based on rules defined by...

1 year ago

[Azure AI Search] Internal Server Error when creating CMK encrypted objects

Scenario Customers follow the Microsoft doc to create CMK encrypted objects (data source, index etc.), but get the 500 Internal Serv...

1 year ago

Optimizing Azure Table Storage: Automated Data Cleanup using a PowerShell script with Azure Automate

Scenario This blog’s aim is to manage Table Storage data efficiently. Imagine you have a large Azure Table Storage that accumulates logs from ...

1 year ago

Optimizing Azure Table Storage: Automated Data Clean-up using a PowerShell script with Azure Automat

Scenario This blog’s aim is to manage Table Storage data efficiently. Imagine you have a large Azure Table Storage that accumulates logs from ...

1 year ago

Restoring Soft-Deleted Blobs with multithreading in Azure Storage Using C#

Blob soft delete is an essential feature that safeguards your data against accidental deletions or overwrites. By retaining deleted data for a...

1 year ago

Performing simple Azure Table Storage REST API operations using curl command.

The blog provides guidance to perform simple Table Storage REST API operations such as Create table, Delete Table, Insert entity, Delete entit...

2 years ago

Bulk delete all the old jobs from the batch account

Deleting a Job also deletes all Tasks that are part of that Job, and all Job statistics. This also overrides the retention period for Task dat...

2 years ago

Utilizing Azure Storage and Runbooks for scheduled automated backups of Azure SQL Databases

In this article, we are going to provide detailed steps to create a scheduled Azure SQL Database backup to storage account using automation. T...

2 years ago

[Azure Service Bus] JMS messages getting dead-lettered

The article discusses a problem where numerous messages end up in the dead letter queue (DLQ) when the JMS service bus consumer connects to th...

2 years ago

Newsletter

Get the latest Dynamics 365 and Power Platform content in your inbox

A curated digest of community blogs, product news, videos, and podcasts — delivered without the noise.

Weekly updates Unsubscribe anytime Fresh community picks
We use your email only for the newsletter and you can unsubscribe at any time.
By subscribing, you agree to the privacy policy.