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

8

22

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?

Leakage answered 6/9, 2019 at 4:25 Comment(1)
Removing messages has become really simple with tools like Serverless360 (Azure monitoring tool), ServiceBusExplorer (Azure Service Bus management tool).Noleta
R
11

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

Rambunctious answered 6/9, 2019 at 8:18 Comment(3)
thank you for your reply! Yes we've used Service Bus Explorer however, this tool can be send/inspect manually, my issue is when I already had message stayed in my queue and I would like to see what they look like, I cannot play with service bus explorer here... It is able to tell me same as how many message I have as active/dead-letter, but won't show me what they look like in detail..Leakage
Sorry it's my bad, I found the way to detect and delete message using service bus explorer now!Leakage
Looks like that MS is not longer providing the download. You can find it instead here: github.com/paolosalvatori/ServiceBusExplorer as also MS themselves are linking to on azure.microsoft.com/en-gb/updates/sesrvice-bus-explorerIrwin
L
7

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

Lateral answered 2/2 at 10:41 Comment(1)
I only get "No messages received" when I do this. Does not work.Ridley
S
4

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

Staircase answered 31/7, 2021 at 7:16 Comment(0)
E
4

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

Eidolon answered 16/11, 2022 at 6:26 Comment(0)
C
2

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.

Clammy answered 6/9, 2019 at 4:48 Comment(7)
thank you for your reply! Regarding your last comment that "write a script to do", could you please refer any documentation or tutorial/example on that?Leakage
I don't have a link to share. When I wrote this down, was referring to the PowerShell option where you can use ASB SDK to achieve the job.Clammy
5.5 years and counting for this feature request! The "good" news is that they have it at the top of their backlog (as of Feb 2022).Americanist
Note to people going this way... You need a connection string which has Manage Send Listen rights so goto Settings >> Shared Access Policies in Azure and create one, then use that connection string ... Otherwise you get an unauthorised 401 in the apps logsHolbrook
@Americanist Surely you didn't expect anything different from Microsoft? At this point it must be painfully obvious for anyone working with Azure that their PaaS offerings are treated as second class citizens, at best.Reluctivity
Its the middle of 2024 and still not implemented. God I miss working in AWS. Azure hate developers...Ridley
Well, you can now batch delete. But it's still not a purge.Clammy
E
1

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

Esbensen answered 9/1, 2023 at 8:46 Comment(1)
Yes, but the native version in Azure Portal is quite limited. Very happy to find that desktop toolIrwin
B
0

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

enter image description here

Bergwall answered 21/9, 2021 at 12:16 Comment(1)
I believe this is the tool being referenced: QueueExplorerAmericanist
T
0

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
Tellurite answered 18/8, 2023 at 14:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.