Drex
Drex

Reputation: 3821

How to delete/clear active/dead-letter messages from Azure Service Bus Queue?

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?

Upvotes: 29

Views: 63045

Answers (10)

Xile
Xile

Reputation: 27

In Azure portal navigate to the queue and the click on "Service Bus Explorer". Make sure you choose "Receive Mode" and then click on "Purge messages". enter image description here

Upvotes: 0

LosManos
LosManos

Reputation: 7692

If you switch the "Built in" Azure Service bus explorer from Peek Mode to Receive Mode you might get two new buttons (depending on rights and if there are messages).
These are Dead-letter and Purge messages.

Even though the latter says "Purge" it might be that it does a Receive-and-delete. Which might have an impact on CPU/RU if you have lots and lots of messages. Might. I lack sources for this claim as it is harvested crud from historicial links and discussions and whatnot.

Dead-letter button Purge messages button

Upvotes: 0

Sean Feldman
Sean Feldman

Reputation: 25994

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.

Upvotes: 3

Rajesh Rajamani
Rajesh Rajamani

Reputation: 531

There is a way in the portal .

  1. Navigate to the topic / subscription

  2. Set the mode to Receive enter image description here

  3. Then Click on Receive Messages and in the pop-up set the option as ReceiveAndDelete

enter image description here

  1. Repeat the process until the DLQ is clear enter image description here

Note : Observe the difference in the messages count between step 2 and step 4 ( shows the messages are deleted ) .

More on this mode here

Upvotes: 19

Manuel
Manuel

Reputation: 1

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

Upvotes: 0

AHMED EL-HAROUNY
AHMED EL-HAROUNY

Reputation: 836

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.

enter image description here

Upvotes: 1

Mauricio Aviles
Mauricio Aviles

Reputation: 1114

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

Upvotes: 4

Dejan Grujić
Dejan Grujić

Reputation: 417

You can delete messages using QueueExplorer, which is a commercial Windows-based tool:

enter image description here

Upvotes: 0

T.R
T.R

Reputation: 166

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

Upvotes: 4

Thili
Thili

Reputation: 768

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 image description here

Enter Connection string which you can find on in Dashboard --> Service Bus --> --> Shared access policies enter image description here

After connected Successfully you will be able to see all the topics queues in the connected servicebus select the Queue that you wanted Access

enter image description here

You Can receive and delete as you wish

enter image description here

Upvotes: 11

Related Questions