3

I have an Azure Service Bus Queue filled with messages and I want to delete a message based on it's ID.

The REST API provides this solution by http request:

DELETE  http{s}://{serviceNamespace}.servicebus.windows.net/{queuePath}/messages/{messageId}/{lockToken}

For the .net 5 sdk, Microsoft provides a solution without using the message's id but rather a receivedMessage object

string connectionString = "<connection_string>";
string queueName = "<queue_name>";

// since ServiceBusClient implements IAsyncDisposable we create it with "await using"
await using var client = new ServiceBusClient(connectionString);

// create a receiver that we can use to receive and settle the message
ServiceBusReceiver receiver = client.CreateReceiver(queueName);

// the received message is a different type as it contains some service set properties
ServiceBusReceivedMessage receivedMessage = await receiver.ReceiveMessageAsync();

// complete the message, thereby deleting it from the service
await receiver.CompleteMessageAsync(receivedMessage);

I want to delete a message by it's id, using the .net sdk.

1 Answers1

4

There's no way to achieve it with a simple command, unfortunately. Some of this was discussed here. You could add your voice to the thread and, hopefully, it will reach the right audience.

Sean Feldman
  • 23,443
  • 7
  • 55
  • 80