Why do my AWS SQS messages not get deleted?

Viewed 860

I have an AWS SQS queue which receives the messages, iterates through them printing the details and then I attempt to delete them. Unfortunately they are not deleting even though I get a success response. I can't figure out why they are not being removed when I am sure I've used similar code before.

The basic example I'm trying is like this:

import boto3

# Create SQS client
sqs = boto3.client('sqs',
                    region_name='',
                    aws_access_key_id='',
                    aws_secret_access_key=''
                    )

queue_url = ''

# Receive message from SQS queue
response = sqs.receive_message(
    QueueUrl=queue_url,
    AttributeNames=[
        'All'
    ],
    MaxNumberOfMessages=10,
    MessageAttributeNames=[
        'All'
    ],
    VisibilityTimeout=0,
    WaitTimeSeconds=0
)

print(len(response['Messages']))

for index, message in enumerate(response['Messages']):
    print("Index Number: ", index)
    print(message)

    receipt_handle = message['ReceiptHandle']

    # do some function

    sqs.delete_message(
         QueueUrl=queue_url,
         ReceiptHandle=receipt_handle
    )
3 Answers

Probably because you are using VisibilityTimeout=0. This means that the message immediately goes back to the SQS queue. So there is nothing to delete for you.

Key points to understand regarding SQS:

  1. Visibility timeout
  2. Wait time seconds
  3. Receipt handle

When a message is received by a Lambda function, it gets a 'ReceiptHandle' for that receive. Now, this ReceiveHandle changes whenever a new lambda thread receives the message making the previous handler invalid.

Since VisibilityTimeout = 0 and WaitTimeSeconds = 0,(as pointed out by @Mandraenke), the message is immediately made available to another lambda thread with a new 'ReceiptHandle'.

The receipt 'ReceiptHandle' being processed by the previous lambda thread becomes invalid and could not be reached by the lambda function processing it.

Related