Reputation: 3202
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
)
Upvotes: 4
Views: 3489
Reputation: 61
Key points to understand regarding SQS:
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.
Upvotes: 1
Reputation: 3266
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.
Upvotes: 1
Reputation: 238051
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.
Upvotes: 3