Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all messages of SQS using Python

I am having a Amazon SQS queue holding n number of messages, I want to delete the messages from the queue using a python code. My current code looks like:

import boto.sqs

sqs = boto.sqs.connect_to_region("ap-southeast-1", aws_access_key_id='XXX', aws_secret_access_key='XXX')
q = sqs.get_queue("grand_torm") #SQS queue name

#text_file = open('download.json', 'w')
m = q.read(visibility_timeout=15)
if m == None:
    print "No message!"
else:
    count = 0 
    while (count < 50):
        q.delete_message(m)
        print "DELETED"

But this only deletes one message at a time, only "DELETED" gets to be printed 50 times. What am I missing here ?

like image 443
Nishant Singh Avatar asked Oct 19 '25 13:10

Nishant Singh


1 Answers

If you want to delete all messages from a queue, you can use q.purge().

Note, this action may take at least 60 seconds to complete. Also, in order for this to work, you must have the sqs:PurgeQueue permission. See the AWS docs and the boto docs for more info.

like image 171
David Morales Avatar answered Oct 21 '25 02:10

David Morales



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!