Is there anyway to delete/clear either the active/dead-letter messages from Azure Service Bus Queue in Azure portal? Currently we've sent a couple of messages to our queue while both the active and some dead-letter messages holds up there for nothing, and our service bus subscriber didn't trigger somehow, so we'd like to delete these messages to make our queue clean again. In order to wait until service bus drop these messages after expiration period, could we manually remove them ourselves?
Using Service Bus Explorer you can connect to Azure Service Bus and administer messaging entities. You can download the tool here.
Once you download the tool you run “ServiceBusExplorer.exe” In the Service Bus Explorer go to File Connect
Enter Connection string which you can find on in Dashboard --> Service Bus --> --> Shared access policies
After connected Successfully you will be able to see all the topics queues in the connected servicebus select the Queue that you wanted Access
You Can receive and delete as you wish
There is a way in the portal .
Navigate to the topic / subscription
Then Click on Receive Messages and in the pop-up set the option as ReceiveAndDelete
Note : Observe the difference in the messages count between step 2 and step 4 ( shows the messages are deleted ) .
More on this mode here
You may call the service bus API for that. Using DELETE method will retrieve and delete messages from queue. Offical document is here. API is
https://{SERVICENAMESPACE}.servicebus.windows.net/{QUEUE_NAME}/$DeadLetterQueue/messages/head
. And
https://{SERVICENAMESPACE}.servicebus.windows.net/{QUEUE_NAME}/messages/head
You can use curl as below to receive and delete message, write a while loop for that can achieve your goal. SAS token can be retrieved by following the offical document.
curl -X DELETE -H "Authorization: SharedAccessSignature sr=<NAMESPACE NAME>.servicebus.windows.net&sig=<SHARED ACCESS KEY>&se=<TOKEN EXPIRY INSTANT>&skn=<SHARED KEY NAME>" ${URL}
Get SAS token code:
get_sas_token() {
eval ${CONNECT_STRING}
local EXPIRY=${EXPIRY:=$((60 * 60 * 1))} # Default token expiry is 1 hour
local ENCODED_URI=$(echo -n ${Endpoint} | jq -s -R -r @uri)
local TTL=$(($(date +%s) + ${EXPIRY}))
local UTF8_SIGNATURE=$(printf "%s\n%s" ${ENCODED_URI} ${TTL} | iconv -t utf8)
local HASH=$(echo -n "${UTF8_SIGNATURE}" | openssl sha256 -hmac ${SharedAccessKey} -binary | base64)
local ENCODED_HASH=$(echo -n ${HASH} | jq -s -R -r @uri)
AUTH_HEADER="SharedAccessSignature sr=${ENCODED_URI}&sig=${ENCODED_HASH}&se=${TTL}&skn=${SharedAccessKeyName}"
}
Delete dead letters queue(you can change URL to delete active messages):
purge_dlq_queue() {
local DLQ_QUEUE_URL="https://${SERVICENAMESPACE}.servicebus.windows.net/${QUEUE_NAME}/\$DeadLetterQueue/messages/head"
local count=1000
echo "cleaning the dead letters messages from the message queue..."
while [[ ${count} -ge 0 ]]
do
local STATUS_CODE=$(curl -I -X DELETE -H "Authorization: ${AUTH_HEADER}" ${DLQ_QUEUE_URL} 2>/dev/null | head -n 1 | cut -d$' ' -f2)
if [[ STATUS_CODE -ge 300 ]]; then
echo "Exit dead letters message queue cleaning with code ${STATUS_CODE}"
return 1
elif [[ STATUS_CODE -eq 204 ]]; then
echo "dead letters message queue has been cleaned"
return 0
fi
let count--
done
echo "Exit with maxium number tries."
return 1
}
The script code can be check from here
You can go to the online storage explorer and "receive and delete" the messages. More details here: https://github.com/Azure/azure-service-bus/issues/1#issuecomment-1261327751
Update: as of June 2024, batch message delete option was added to allow deleting multiple messages. While not a purge, this is a step closer and allows purging implementation. Important factors are the maximum number of messages that can be deleted and non-deterministic number of messages deleted vs requested for deletion.
Is there anyway to delete/clear either the active/dead-letter messages from Azure Service Bus Queue in Azure portal?
Purge operation is not currently supported. There's a feature request to implement purging, but it hasn't been implemented.
You could use some tools to perform purge-like operation. ServiceBus Explorer can purge messages (Receive and Delete option) on regular and dead-letter queues.
Alternatively, you could write a script to do that as well.
This is now available natively in Azure Portal: https://learn.microsoft.com/en-us/azure/service-bus-messaging/explorer
Or as an alternative you can use Service Bus Cloud Explorer which runs in the browser with logged-in user Azure account by default and have a delete and purge functionality.
I struggled two hours for this matter and I found out the easiest solution.
In order to clear the list of messages, You just need to:
- Download Service Bus Explorer from here
- Open it
- Connect to your queue using the primary key ("Endpoint=sb://<
NAMESPACE
>.servicebus.windows.net/;SharedAccessKeyName=<POLICY_NAME
>;SharedAccessKey=<KEY
>;EntityPath=<QUEUE_NAME
>") - Click on Purge All Messages, click for the UI screenshot
© 2022 - 2024 — McMap. All rights reserved.