Deleting a Message

Once you receive the message, you must delete it from the queue to acknowledge that you processed the message and no longer need it. You specify which message to delete by providing the receipt handle that Amazon SQS returned when you received the message. You can delete only one message per call. You can delete an entire queue with a call to DeleteQueue, even if the queue has messages in it.

[Note]Note

If you don't have the receipt handle for the message, you can call ReceiveMessage again and receive the message again. Each time you receive the message, you get a different receipt handle. Use the latest receipt handle when calling DeleteMessage, otherwise your message might not be deleted from the queue.

The following code snippets demonstrate how to delete the message from your MyQueue queue.

Java

To run the sample

  1. Open DeleteMessageSample.java.

  2. Locate the following lines.

    DeleteMessageRequest request = new DeleteMessageRequest();
            
    // @TODO: set request parameters here
    
    // invokeDeleteMessage(service, request);
  3. Replace the lines with the following new lines of code.

    DeleteMessageRequest request = new DeleteMessageRequest();
    request.setQueueName("MyQueue");
    request.setReceiptHandle("Receipt handle you received from ReceiveMessage call");
    invokeDeleteMessage(service, request);
  4. Compile and run the sample.

    The message is deleted from the MyQueue queue. The response includes the request ID that Amazon SQS assigned to your request.

C#

To run the sample

  1. Open AmazonSQSSamples.cs.

  2. Comment out the code you added in the previous section (that code consisted of the following lines).

    ReceiveMessage request = new ReceiveMessageRequest();
    request.QueueName = "MyQueue";
    ReceiveMessageSample.InvokeReceiveMessage(service, request);
  3. Locate the following lines.

    // DeleteMessageRequest request = new DeleteMessageRequest();
    // @TODO: set request parameters here
    // DeleteMessageSample.InvokeDeleteMessage(service, request);
  4. Replace the lines with the following new lines of code.

    DeleteMessageRequest request = new DeleteMessageRequest();
    request.QueueName = "MyQueue";
    request.ReceiptHandle = "Receipt handle you received from ReceiveMessage call";
    DeleteMessageSample.InvokeDeleteMessage(service, request);
  5. Run the sample.

    The message is deleted from the MyQueue queue. The response includes the request ID that Amazon SQS assigned to your request.

Perl

To run the sample

  1. Open DeleteMessageSample.pl.

  2. Locate the following line.

    # invokeDeleteMessage($service, $request);
  3. Replace the line with the following new lines of code.

    my $request = Amazon::SQS::Model::DeleteMessageRequest->new({
    QueueName => "MyQueue",
    ReceiptHandle => "Receipt handle you received from ReceiveMessage call"
    });
    invokeDeleteMessage($service, $request);
  4. Run the sample.

    The message is deleted from the MyQueue queue. The response includes the request ID that Amazon SQS assigned to your request.

PHP5

To run the sample

  1. Open DeleteMessageSample.php.

  2. Locate the following line.

    // invokeDeleteMessage($service, $request);
  3. Replace the line with the following new lines of code.

    require_once ('Amazon/SQS/Model/DeleteMessageRequest.php');
    $request = new Amazon_SQS_Model_DeleteMessageRequest();
    $request->setQueueName('MyQueue');
    $request->setReceiptHandle('Receipt handle you received from ReceiveMessage call');
    invokeDeleteMessage($service, $request);
  4. Run the sample.

    The message is deleted from the MyQueue queue. The response includes the request ID that Amazon SQS assigned to your request.

VB.NET

To run the sample

  1. Open AmazonSQSSamples.vb.

  2. Comment out the code you added in the previous section (that code consisted of the following lines).

    Dim request As New ReceiveMessageRequest()
    request.QueueName = "MyQueue"
    ReceiveMessageSample.InvokeReceiveMessage(service, request)
  3. Locate the following lines.

    ' Dim request As New DeleteMessageRequest()
    ' @TODO: set request parameters here
    ' DeleteMessageSample.InvokeDeleteMessage(service, request)
  4. Replace the lines with the following new lines of code.

    Dim request As New DeleteMessageRequest()
    request.QueueName = "MyQueue"
    request.ReceiptHandle = "Receipt handle you received from ReceiveMessage call"
    DeleteMessageSample.InvokeDeleteMessage(service, request)
  5. Run the sample.

    The message is deleted from the MyQueue queue. The response includes the request ID that Amazon SQS assigned to your request.

Scratchpad

To run the sample

  1. In the scratchpad, select DeleteMessage from the Explore API list box.

  2. Enter MyQueue in the Queue Name field.

  3. Enter the receipt handle (which you received from the preceding ReceiveMessage call) in the Receipt Handle field.

  4. Select one of the following:

    • To invoke the request, click Invoke Request. Amazon SQS returns a response.

    • To view the signed URL, click Display Signed URL. Then, copy and paste the signed URL into a browser. Amazon SQS returns a response.

    • To view the string to sign, click Display String to Sign.