Nishant Singh
Nishant Singh

Reputation: 3209

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 ?

Upvotes: 4

Views: 7737

Answers (2)

yogesh motwani
yogesh motwani

Reputation: 1

If you want to do it only once, you can do it manually by logging into your AWS account -> sqs -> your sqs queue -> purge.

Upvotes: 0

David Morales
David Morales

Reputation: 910

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.

Upvotes: 9

Related Questions