19

During development, I'm generating a lot of bogus messages on my Amazon SQS. I was about to write a tiny app to delete all the messages (something I do frequently during development). Does anyone know of a tool to purge the queue?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Hairgami_Master
  • 5,429
  • 10
  • 45
  • 66

9 Answers9

50

If you don't want to write script or delete your queue. You can change the queue configuration:

  1. Right click on queue > configure queue
  2. Change Message Retention period to 1 minute (the minimum time it can be set to).
  3. Wait a while for all the messages to disappear.

I found that this way works well for deleting all messages in a queue without deleting the queue.

Stephan
  • 16,509
  • 7
  • 35
  • 61
rhinoinrepose
  • 2,110
  • 2
  • 19
  • 26
  • "Value must be between 1 minute and 14 days." I set mine to 1 minute and needed to wait maybe 10 minutes for messages to finally disappear from https://console.aws.amazon.com/sqs/home?region=us-east-1 It's a great trick! Thanks! See also http://stackoverflow.com/a/7893892/470749 – Ryan Jun 30 '14 at 20:45
  • 2
    Handy trick, but there is now a **Purge Queue** option under Queue Actions – Jamie Hall Dec 19 '18 at 12:56
11

As of December 2014, the sqs console now has a purge queue option in the queue actions menu.

Paul Blakey
  • 667
  • 10
  • 13
  • http://aws.amazon.com/about-aws/whats-new/2014/12/08/delete-all-messages-in-an-amazon-sqs-queue/ – Ramson Tutte Jan 28 '15 at 13:10
  • Tried that from AWS console on an SQS queue with delay property set and clients long-polling that queue. PurgeQueue didn't purge the messages. Had to stop the long-polling clients and manually delete those messages. – Ramson Tutte Jan 28 '15 at 22:43
8

For anyone who has come here, looking for a way to delete SQS messages en masse in C#...

//C# Console app which deletes all messages from a specified queue
//AWS .NET library required.

using System;
using System.Net;
using System.Configuration;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Text;

using Amazon;
using Amazon.SQS;
using Amazon.SQS.Model;
using System.Timers;

using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Diagnostics;

namespace QueueDeleter
{
    class Program
    {
        public static System.Timers.Timer myTimer;
        static NameValueCollection appConfig = ConfigurationManager.AppSettings;
        static string accessKeyID = appConfig["AWSAccessKey"];
        static string secretAccessKeyID = appConfig["AWSSecretKey"];
        static private AmazonSQS sqs;

        static string myQueueUrl = "https://queue.amazonaws.com/1640634564530223/myQueueUrl";
        public static String messageReceiptHandle;

        public static void Main(string[] args)
        {
            sqs = AWSClientFactory.CreateAmazonSQSClient(accessKeyID, secretAccessKeyID);

            myTimer = new System.Timers.Timer();
            myTimer.Interval = 10;
            myTimer.Elapsed += new ElapsedEventHandler(checkQueue);
            myTimer.AutoReset = true;
            myTimer.Start();
            Console.Read();
        }

        static void checkQueue(object source, ElapsedEventArgs e)
        {
            myTimer.Stop();

            ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest();
            receiveMessageRequest.QueueUrl = myQueueUrl;
            ReceiveMessageResponse receiveMessageResponse = sqs.ReceiveMessage(receiveMessageRequest);
            if (receiveMessageResponse.IsSetReceiveMessageResult())
            {
                ReceiveMessageResult receiveMessageResult = receiveMessageResponse.ReceiveMessageResult;

                if (receiveMessageResult.Message.Count < 1)
                {
                    Console.WriteLine("Can't find any visible messages.");
                    myTimer.Start();
                    return;
                }

            foreach (Message message in receiveMessageResult.Message)
            {
                Console.WriteLine("Printing received message.\n");

                messageReceiptHandle = message.ReceiptHandle;

                Console.WriteLine("Message Body:");
                if (message.IsSetBody())
                {
                    Console.WriteLine("    Body: {0}", message.Body);
                }
                sqs.DeleteMessage(new DeleteMessageRequest().WithQueueUrl(myQueueUrl).WithReceiptHandle(messageReceiptHandle));
            }
        }
        else
        {
            Console.WriteLine("No new messages.");
        }

         myTimer.Start();
        }
    }
}
Hairgami_Master
  • 5,429
  • 10
  • 45
  • 66
  • at this point it would probably be best to use the [AWS SQS CLI](http://docs.aws.amazon.com/cli/latest/reference/index.html#cli-aws) to do these sort of one-off commands – Don Cheadle May 04 '15 at 18:54
5

Check the first item in queue. Scroll down to last item in queue. Hold shift, click on item. All will be selected.

user3557141
  • 67
  • 1
  • 2
4

I think the best way would be to delete the queue and create it again, just 2 requests.

Pradeep
  • 3,093
  • 17
  • 21
  • 1
    Thanks Pradeep- good answer, but I can't risk the queue being unavailable. I'm not sure how AWS deletes and recreates, names URLs, etc.. Perhaps this is perfectly fine, and is a nearly instantaneous deletion and recreation, I'm just too scared to try it. – Hairgami_Master Dec 13 '11 at 21:31
  • 12
    It's worth noting that after you delete a queue, SQS doesn't let you create a queue with the same name within 60 seconds. – teedyay Jun 27 '12 at 09:20
  • This works, but isn't good for automation due to the 60 second delay – Stephan Jun 03 '14 at 20:18
1

I think best way is changing Retention period to 1 minute, but here is Python code if someone needs:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import boto.sqs
from boto.sqs.message import Message
import time
import os

startTime = program_start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())

### Lets connect to SQS:
qcon = boto.sqs.connect_to_region(region,aws_access_key_id='xxx',aws_secret_access_key='xxx')
SHQueue = qcon.get_queue('SQS')
m = Message()
### Read file and write to SQS
counter = 0
while counter < 1000:   ## For deleting 1000*10 items, change to True if you want delete all
    links = SHQueue.get_messages(10)
    for link in links:
            m = link
            SHQueue.delete_message(m)
    counter += 1
#### The End
print "\n\nTerminating...\n"
print "Start: ", program_start_time
print "End time: ", time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
Emin Mastizada
  • 1,375
  • 2
  • 15
  • 30
1

Option 1: boto sqs has a purge_queue method for python:

purge_queue(queue)
Purge all messages in an SQS Queue.

Parameters: queue (A Queue object) – The SQS queue to be purged
Return type:    bool
Returns:    True if the command succeeded, False otherwise

Source: http://boto.readthedocs.org/en/latest/ref/sqs.html

Code that works for me:

conn = boto.sqs.connect_to_region('us-east-1',
          aws_access_key_id=AWS_ACCESS_KEY_ID,
          aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
)
q = conn.create_queue("blah")
#add some messages here

#invoke the purge_queue method of the conn, and pass in the
#queue to purge.
conn.purge_queue(self.queue)

For me, it deleted the queue. However, Amazon SQS only lets you run this once every 60 seconds. So I had to use the secondary solution below:

Option 2: Do a purge by consuming all messages in a while loop and throwing them out:

    all_messages = []
    rs = self.queue.get_messages(10)
    while len(rs) > 0:
        all_messages.extend(rs)
        rs = self.queue.get_messages(10)
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
0

If you have access to the AWS console, you can purge a queue using the Web UI.

Steps:

  • Navigate to Services -> SQS
  • Filter queues by your "QUEUE_NAME"
  • Right-click on your queue name -> Purge queue

This will request for the queue to be cleared and this should be completed with 5 or 10 seconds or so.

See below for how to perform this operation:

Purge queue manually

anataliocs
  • 10,427
  • 6
  • 56
  • 72
0

To purge an SQS from the API see:

https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_PurgeQueue.html

Ayoub
  • 361
  • 4
  • 15