5

According to the Amazon documentation, the code to delete a single message from the SQS Queue is:

    // Delete a message
System.out.println("Deleting a message.\n");
String messageReceiptHandle = messages.get(0).getReceiptHandle();
sqs.deleteMessage(new DeleteMessageRequest()
    .withQueueUrl(myQueueUrl)
    .withReceiptHandle(messageReceiptHandle));

However, say I have more than 20 or so messages, do I really have to process this for every single message? Or is there a way to do a batch delete?

Thanks for any insights.

1
  • Why the downvote? It seems a good question Commented Nov 25, 2016 at 16:56

4 Answers 4

9

If you want to delete all messages you don't need batch delete, you should Purge the queue like so:

var request = new Amazon.SQS.Model.PurgeQueueRequest
{
    QueueUrl = "your queue url"
};
_sqsClient.PurgeQueue(request);

Purging a queue removes all the messages already in the queue

Sign up to request clarification or add additional context in comments.

Comments

2

For completeness as this Q&A comes up still when searching and Java 2 API examples are hard to find, here is the purge example in Java:

SqsClient sqs = SqsClient.builder()
         .region(Region.EU_WEST_1)
         .credentialsProvider(ProfileCredentialsProvider.create("default"))
         .build();


PurgeQueueRequest pqRequest = PurgeQueueRequest.builder()
            .queueUrl("your SQS queu URL (not ARN!)")
            .build();

sqs.purgeQueue(pqRequest);

Comments

1

Just looking at the API for doing something similar DeleteMessageBatchRequest seems to do the trick.

1 Comment

Ah, I thought there may have been docs, but I couldn't find what I was looking for. Cheers for that
0

If you look at the SDK documentation, you will see a deleteMessageBatch method which takes a list of messages.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.