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:
- Azure Service Bus client library samples for .NET (latest)
- Azure Service Bus client library samples for Java (latest)
- Azure Service Bus client library samples for Python
- Azure Service Bus client library samples for JavaScript
- Azure Service Bus client library samples for TypeScript
Samples for the older .NET and Java client libraries:
- Azure Service Bus client library samples for .NET (legacy) - See the Prefetch sample.
- Azure Service Bus client library samples for Java (legacy) - See the Prefetch sample.
On 30 September 2026, we'll retire the Azure Service Bus SDK libraries WindowsAzure.ServiceBus, Microsoft.Azure.ServiceBus, and com.microsoft.azure.servicebus, which don't conform to Azure SDK guidelines. We'll also end support of the SBMP protocol, so you'll no longer be able to use this protocol after 30 September 2026. Migrate to the latest Azure SDK libraries, which offer critical security updates and improved capabilities, before that date.
Although the older libraries can still be used beyond 30 September 2026, they'll no longer receive official support and updates from Microsoft. For more information, see the support retirement announcement.
Feedback
https://aka.ms/ContentUserFeedback.
Disponibbli dalwaqt: Matul l-2024 se nkunu qed inwaqqfu gradwalment Problemi GitHub bħala l-mekkaniżmu ta’ feedback għall-kontenut u se nibdluh b’sistema ġdida ta’ feedback. Għal aktar informazzjoni, ara:Issottometti u ara feedback għal