Menu
Amazon Simple Queue Service
Developer Guide

Tutorial: Receiving and Deleting a Message from an Amazon SQS Queue

After you send a message into a queue, you can consume it from the queue. When you request a message from a queue, you can't specify which message to get. Instead, you specify the maximum number of messages (up to 10) that you want to get.

Note

Because Amazon SQS is a distributed system, a queue with very few messages might display an empty response to a receive request. In this case, you can rerun the request to get your message. Depending on your application's needs, you might have to use short or long polling to receive messages.

Amazon SQS doesn't automatically delete a message after receiving it for you, in case you don't successfully receive the message (for example, the consumers can fail or lose connectivity). To delete a message, you must send a separate request which acknowledges that you no longer need the message because you've successfully received and processed it.

The following example demonstrates receiving and deleting a message.

AWS Management Console

  1. Sign in to the AWS Management Console and open the Amazon SQS console at https://console.aws.amazon.com/sqs/.

  2. From the queue list, select a queue.

  3. From the Queue Actions drop-down list, select View/Delete Messages.

    The View/Delete Messages in QueueName dialog box is displayed.

    Note

    The first time you take this action, an information screen is displayed. To hide the screen, check the Don't show this again checkbox.

  4. Choose Start Polling for messages.

    Amazon SQS begins to poll the messages in the queue. The dialog box displays a message from the queue. A progress bar at the bottom of the dialog box displays the status of the message's visibility timeout.

    The following example shows the Message Group ID, Message Deduplication ID, and Sequence Number columns specific to FIFO queues.

  5. Before the visibility timeout expires, select the message that you want to delete and then choose Delete 1 Message.

    The Delete Messages dialog box is displayed.

  6. Confirm that the message you want to delete is checked and choose Yes, Delete Checked Messages.

    The selected message is deleted.

    When the progress bar is filled in, the visibility timeout expires and the message becomes visible to consumers.

  7. Select Close.

Java

To specify the message to delete, provide the receipt handle that Amazon SQS returned when you received the message. You can delete only one message per action. To delete an entire queue, you must use the DeleteQueue action. (You can delete an entire queue even if the queue has messages in it.)

Note

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

Before you begin working with the example code, specify your AWS credentials. For more information, see Set Up AWS Credentials for Development in the AWS SDK for Java Developer Guide.

To receive and delete a message a message from a standard queue

  1. Copy the example program.

    The following section of the code receives a message from your queue:

    System.out.println("Receiving messages from MyQueue.\n");
    ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl);
    List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages();
    for (Message message : messages) {
        System.out.println("  Message");
        System.out.println("    MessageId:     " + message.getMessageId());
        System.out.println("    ReceiptHandle: " + message.getReceiptHandle());
        System.out.println("    MD5OfBody:     " + message.getMD5OfBody());
        System.out.println("    Body:          " + message.getBody());
        for (Entry<String, String> entry : message.getAttributes().entrySet()) {
            System.out.println("  Attribute");
            System.out.println("    Name:  " + entry.getKey());
            System.out.println("    Value: " + entry.getValue());
        }
    }
    System.out.println();

    The following section of the code deletes the message:

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

    The queue is polled and returns 0 or more messages. The example prints the following items:

    • The message ID that you received when you sent the message to the queue.

    • The receipt handle that you later use to delete the message.

    • An MD5 digest of the message body (for more information, see RFC1321).

    • The message body.

    • The request ID that Amazon SQS assigned to your request

    If no messages are received in this particular call, the response includes only the request ID.

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

To receive and delete a message a message from a FIFO queue

  1. Copy the example program.

    The following section of the code receives a message from your queue:

    // Receive messages
    System.out.println("Receiving messages from MyFifoQueue.fifo.\n");
    ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl);
    // Uncomment the following to provide the ReceiveRequestDeduplicationId
    //receiveMessageRequest.setReceiveRequestAttemptId("1");
    List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages();
    for (Message message : messages) {
        System.out.println("  Message");
        System.out.println("    MessageId:     " + message.getMessageId());
        System.out.println("    ReceiptHandle: " + message.getReceiptHandle());
        System.out.println("    MD5OfBody:     " + message.getMD5OfBody());
        System.out.println("    Body:          " + message.getBody());
        for (Entry<String, String> entry : message.getAttributes().entrySet()) {
            System.out.println("  Attribute");
            System.out.println("    Name:  " + entry.getKey());
            System.out.println("    Value: " + entry.getValue());
        }
    }
    System.out.println();

    The following section of the code deletes the message:

    // Delete the message
    System.out.println("Deleting the message.\n");
    String messageReceiptHandle = messages.get(0).getReceiptHandle();
    sqs.deleteMessage(new DeleteMessageRequest(myQueueUrl, messageReceiptHandle));
  2. Compile and run the example.

    The message is received and deleted.