Skip to content

Latest commit

 

History

History
56 lines (36 loc) · 4.32 KB

batch-delete.md

File metadata and controls

56 lines (36 loc) · 4.32 KB
title description ms.topic ms.date
Delete messages from Azure Service Bus
This article explains how to delete messages in Azure Service Bus programmatically.
article
05/20/2024

Batch delete messages in Azure Service Bus (Preview)

Azure Service Bus is a fully managed enterprise integration message broker that enables you to send and receive messages between decoupled applications and services. However, sometimes you might want to delete messages from a queue or subscription without processing them, for example, if they're expired, corrupted, or irrelevant. This article shows you how to delete messages in batches in Azure Service Bus.

Scenarios for Batch deletion of messages

There are several scenarios where you might want to use the batch delete messages feature in Azure Service Bus. Some of them are:

  • Expired Messages: Delete messages that exceed their time to live (TTL) value and are in the dead-letter queue.
  • Failed Validation or Processing: Remove messages that failed validation or processing logic and are in the dead-letter queue.
  • Irrelevant Messages: Delete messages no longer relevant for your application logic from the active queue.
  • Handling Duplicates or Incorrect Content: Remove duplicate or incorrect messages from the active queue.

By using the batch delete messages feature, you can delete multiple messages from a queue or subscription in one operation, instead of deleting them one by one. Since deletion is done at service side, you don't need to receive the messages before deleting them. This method minimizes both the number of service requests and network latency.

Important

Currently, Batch delete is not supported with partitioned entities. You can delete a maximum of 4000 messages in a batch delete call. Batch deletion is done on a best-effort basis and doesn’t guarantee the exact messageCount will be deleted in single API call.

How to batch delete messages in Service Bus

You can delete messages by calling DeleteMessagesAsync on Service Bus Receiver object. On the server side, DeleteMessagesAsync requires two parameters: messageCount and beforeEnqueueTime.

  • messageCount: The desired number of messages to delete. The service might delete fewer messages than this limit.
  • beforeEnqueueTime: An optional DateTimeOffset, in UTC, representing the cutoff time for deletion. Only messages that were enqueued before this time are deleted.

Additionally, you can call PurgeMessagesAsync to purge all messages from entity.

When using Azure SDKs to perform these operations, the beforeEnqueueTime parameter defaults to the current UTC time (DateTime.UtcNow()). It’s important to ensure you provide the correct values to prevent unintended message deletion.

Note

The purge operation could lead to increased CPU usage as it involves multiple API calls. During purge, locked messages are not eligible for removal and will remain in the entity.

Next steps

To explore Azure Service Bus features, try the samples in language of your choice:

Samples for the older .NET and Java client libraries:

[!INCLUDE service-bus-track-0-and-1-sdk-support-retirement]