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
)
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With