Delete All Messages in an Amazon SQS Queue via AWS CLI
Amazon SQS or Simple Queue Service is a fast, reliable, scalable, fully managed message queuing service. There is also AWS CLI or Command Line Interface available to use with the service.
If you have a lot of messages in a queue, this command will show the approximate number:
|
|
Where $url
is the URL to the Amazon SQS queue.
There is no command to delete all messages yet, but you can chain a few commands together to make it work:
|
|
Amazon SQS allows you to receive up to 10 messages at most, so you have to loop through it. The default response of the receive-message
command is in JSON, hence using jq
to process the result and extract just what we need for deletion: MessageId
and ReceiptHandle
.
If there are no more messages, you will likely get the following error:
|
|
Then you exit the loop.
With the network latency and limitation of 10 messages at a batch, this is pretty slow. But you can run multiple commands to speed up the deletion process. Or take the shortcut with a faster way: delete the entire queue and then recreate it.