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?
3 Answers
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
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.
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