'Azure service bus delete queue message by id using the .net sdk

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.



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sean Feldman