0
0
AwsHow-ToBeginner · 3 min read

How to Delete a Message from AWS SQS Queue

To delete a message from an AWS SQS queue, use the DeleteMessage API call with the queue URL and the message's ReceiptHandle. The ReceiptHandle is a unique identifier received when you read the message, and it must be provided to remove that specific message from the queue.
📐

Syntax

The DeleteMessage operation requires two main inputs:

  • QueueUrl: The URL of the SQS queue.
  • ReceiptHandle: The unique identifier for the message to delete, obtained when the message is received.

Deleting a message removes it permanently from the queue so it won't be delivered again.

plaintext
DeleteMessage {
  QueueUrl: string,
  ReceiptHandle: string
}
💻

Example

This example shows how to delete a message from an SQS queue using AWS SDK for JavaScript (v3). It first receives a message, then deletes it using its ReceiptHandle.

javascript
import { SQSClient, ReceiveMessageCommand, DeleteMessageCommand } from "@aws-sdk/client-sqs";

const client = new SQSClient({ region: "us-east-1" });
const queueUrl = "https://sqs.us-east-1.amazonaws.com/123456789012/MyQueue";

async function deleteMessage() {
  // Receive one message
  const receiveParams = { QueueUrl: queueUrl, MaxNumberOfMessages: 1 };
  const receiveCommand = new ReceiveMessageCommand(receiveParams);
  const data = await client.send(receiveCommand);

  if (!data.Messages || data.Messages.length === 0) {
    console.log("No messages to delete.");
    return;
  }

  const message = data.Messages[0];
  console.log("Received message:", message.Body);

  // Delete the message using ReceiptHandle
  const deleteParams = { QueueUrl: queueUrl, ReceiptHandle: message.ReceiptHandle };
  const deleteCommand = new DeleteMessageCommand(deleteParams);
  await client.send(deleteCommand);

  console.log("Message deleted successfully.");
}

deleteMessage().catch(console.error);
Output
Received message: Hello from SQS! Message deleted successfully.
⚠️

Common Pitfalls

  • Using MessageId instead of ReceiptHandle: The MessageId is not valid for deletion; you must use the ReceiptHandle received when the message was read.
  • Deleting without receiving the message first: You cannot delete a message without its ReceiptHandle, so always receive the message before deleting.
  • Not handling empty queue: If no messages are available, attempting to delete will fail; check for messages before deleting.
javascript
/* Wrong: Using MessageId instead of ReceiptHandle */
const deleteParamsWrong = { QueueUrl: queueUrl, ReceiptHandle: message.MessageId };

/* Right: Use ReceiptHandle */
const deleteParamsRight = { QueueUrl: queueUrl, ReceiptHandle: message.ReceiptHandle };
📊

Quick Reference

Remember these key points when deleting messages from SQS:

  • Always use the ReceiptHandle from the received message.
  • Delete messages promptly after processing to avoid duplicates.
  • Check for messages before attempting deletion.

Key Takeaways

Use the message's ReceiptHandle, not MessageId, to delete from SQS.
DeleteMessage requires the queue URL and the ReceiptHandle as inputs.
Always receive the message first to get its ReceiptHandle before deleting.
Deleting a message removes it permanently from the queue.
Check if messages exist before trying to delete to avoid errors.