Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do my AWS SQS messages not get deleted?

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
    )
like image 591
Johnny John Boy Avatar asked Sep 21 '25 08:09

Johnny John Boy


2 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.

like image 119
Marcin Avatar answered Sep 22 '25 22:09

Marcin


You are setting VisibilityTimeout=0 and WaitTimeSeconds=0 - the message will timeout and become visible again after zero seconds.

This is probably not what you want - you should try with higher values here and read the docs about them: https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-visibility-timeout.html

You can time out the usual processing time and set the values to safe ones, so that messages will be delivered in case of errors.

like image 23
Mandraenke Avatar answered Sep 22 '25 20:09

Mandraenke