Skip to content

delete_message

delete_message(
    queue_url: String,
    receipt_handle: String,
    *,
    config_overrides: Optional[SQSClientConfig] = None,
) -> None

Deletes the specified message from the specified queue. To select the message to delete, use the ReceiptHandle of the message (not the MessageId which you receive when you send the message). Amazon SQS can delete a message from a queue even if a visibility timeout setting causes the message to be locked by another consumer. Amazon SQS automatically deletes messages left in a queue longer than the retention period configured for the queue.

Each time you receive a message, meaning when a consumer retrieves a message from the queue, it comes with a unique ReceiptHandle. If you receive the same message more than once, you will get a different ReceiptHandle each time. When you want to delete a message using the DeleteMessage action, you must use the ReceiptHandle from the most recent time you received the message. If you use an old ReceiptHandle, the request will succeed, but the message might not be deleted.

For standard queues, it is possible to receive a message even after you delete it. This might happen on rare occasions if one of the servers which stores a copy of the message is unavailable when you send the request to delete the message. The copy remains on the server and might be returned to you during a subsequent receive request. You should ensure that your application is idempotent, so that receiving a message more than once does not cause issues.

Parameters:

  • queue_url (String) –

    The URL of the Amazon SQS queue from which messages are deleted.

    Queue URLs and names are case-sensitive.

  • receipt_handle (String) –

    The receipt handle associated with the message to delete.

Raises:

  • InvalidAddress –

    The specified ID is invalid.

  • InvalidIdFormat –

    The specified receipt handle isn't valid for the current version.

  • InvalidSecurity –

    The request was not made over HTTPS or did not use SigV4 for signing.

  • QueueDoesNotExist –

    Ensure that the QueueUrl is correct and that the queue has not been deleted.

  • ReceiptHandleIsInvalid –

    The specified receipt handle isn't valid.

  • RequestThrottled –

    The request was denied due to request throttling.

    • Exceeds the permitted request rate for the queue or for the recipient of the request.

    • Ensure that the request rate is within the Amazon SQS limits for sending messages. For more information, see Amazon SQS quotas in the Amazon SQS Developer Guide.

  • UnsupportedOperation –

    Error code 400. Unsupported operation.

  • UnknownServiceError –

    The service returned an error code this client does not model.

Source code in docs_src/capo_sqs/_services/sqs.py
def delete_message(
    self,
    queue_url: "capo_sqs.types.string.String",
    receipt_handle: "capo_sqs.types.string.String",
    *,
    config_overrides: Optional[SQSClientConfig] = None,
) -> None:
    """<p>Deletes the specified message from the specified queue. To select the message to delete, use the <code>ReceiptHandle</code> of the message (<i>not</i> the <code>MessageId</code> which you receive when you send the message). Amazon SQS can delete a message from a queue even if a visibility timeout setting causes the message to be locked by another consumer. Amazon SQS automatically deletes messages left in a queue longer than the retention period configured for the queue. </p> <note> <p>Each time you receive a message, meaning when a consumer retrieves a message from the queue, it comes with a unique <code>ReceiptHandle</code>. If you receive the same message more than once, you will get a different <code>ReceiptHandle</code> each time. When you want to delete a message using the <code>DeleteMessage</code> action, you must use the <code>ReceiptHandle</code> from the most recent time you received the message. If you use an old <code>ReceiptHandle</code>, the request will succeed, but the message might not be deleted. </p> <p>For standard queues, it is possible to receive a message even after you delete it. This might happen on rare occasions if one of the servers which stores a copy of the message is unavailable when you send the request to delete the message. The copy remains on the server and might be returned to you during a subsequent receive request. You should ensure that your application is idempotent, so that receiving a message more than once does not cause issues.</p> </note>

    Args:
        queue_url: <p>The URL of the Amazon SQS queue from which messages are deleted.</p> <p>Queue URLs and names are case-sensitive.</p>
        receipt_handle: <p>The receipt handle associated with the message to delete.</p>

    Raises:
        capo_sqs.errors.invalid_address.InvalidAddress: <p>The specified ID is invalid.</p>
        capo_sqs.errors.invalid_id_format.InvalidIdFormat: <p>The specified receipt handle isn't valid for the current version.</p>
        capo_sqs.errors.invalid_security.InvalidSecurity: <p>The request was not made over HTTPS or did not use SigV4 for signing.</p>
        capo_sqs.errors.queue_does_not_exist.QueueDoesNotExist: <p>Ensure that the <code>QueueUrl</code> is correct and that the queue has not been deleted.</p>
        capo_sqs.errors.receipt_handle_is_invalid.ReceiptHandleIsInvalid: <p>The specified receipt handle isn't valid.</p>
        capo_sqs.errors.request_throttled.RequestThrottled: <p>The request was denied due to request throttling.</p> <ul> <li> <p>Exceeds the permitted request rate for the queue or for the recipient of the request.</p> </li> <li> <p>Ensure that the request rate is within the Amazon SQS limits for sending messages. For more information, see <a href="https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-quotas.html#quotas-requests">Amazon SQS quotas</a> in the <i>Amazon SQS Developer Guide</i>.</p> </li> </ul>
        capo_sqs.errors.unsupported_operation.UnsupportedOperation: <p>Error code 400. Unsupported operation.</p>
        capo_sqs.errors.UnknownServiceError: The service returned an error code this client does not model.
    """

    def _handler(
        req: "OperationRequest[capo_sqs.types.delete_message_request.DeleteMessageRequest]",
    ) -> OperationResponse[None]:
        import capo_sqs._operations.amazon_sqs.delete_message

        output, http_response = (
            capo_sqs._operations.amazon_sqs.delete_message.delete_message(
                req.options, req.input
            )
        )
        return OperationResponse(output=output, response=http_response)

    interceptors_, options_ = self.operation_options(
        config_overrides, OperationLogging("DeleteMessage")
    )
    input_: capo_sqs.types.delete_message_request.DeleteMessageRequest = {
        "queue_url": queue_url,
        "receipt_handle": receipt_handle,
    }

    response = execute_pipeline(
        OperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    response.response.close()
    return response.output