How to Set Retention Period for AWS SQS Queues
To set the retention period for an AWS SQS queue, configure the
MessageRetentionPeriod attribute, which defines how long messages stay in the queue before deletion. You can set this value from 60 seconds (1 minute) up to 1209600 seconds (14 days) using the AWS Management Console, AWS CLI, or SDKs.Syntax
The retention period is set by specifying the MessageRetentionPeriod attribute in seconds when creating or updating an SQS queue.
Key parts:
QueueUrl: The URL of the SQS queue.Attributes: A map whereMessageRetentionPeriodis set to a string representing seconds.- Value range: 60 (1 minute) to 1209600 (14 days).
bash
aws sqs set-queue-attributes --queue-url <QueueUrl> --attributes MessageRetentionPeriod=<seconds>
Example
This example sets the retention period of an existing SQS queue to 1 hour (3600 seconds) using AWS CLI.
bash
aws sqs set-queue-attributes --queue-url https://sqs.us-east-1.amazonaws.com/123456789012/MyQueue --attributes MessageRetentionPeriod=3600Output
No output if successful; the command sets the retention period to 3600 seconds.
Common Pitfalls
- Setting
MessageRetentionPeriodoutside the allowed range (less than 60 or more than 1209600 seconds) causes errors. - Forgetting to specify the queue URL or using an incorrect URL will fail the command.
- Not having proper permissions to modify queue attributes will cause access denied errors.
- Confusing
VisibilityTimeoutwith retention period; they control different behaviors.
bash
aws sqs set-queue-attributes --queue-url https://sqs.us-east-1.amazonaws.com/123456789012/MyQueue --attributes MessageRetentionPeriod=30 # This will fail because 30 seconds is below the minimum allowed. # Correct usage: aws sqs set-queue-attributes --queue-url https://sqs.us-east-1.amazonaws.com/123456789012/MyQueue --attributes MessageRetentionPeriod=600
Quick Reference
| Attribute | Description | Allowed Values | Default |
|---|---|---|---|
| MessageRetentionPeriod | How long messages stay in the queue before deletion | 60 to 1209600 seconds (1 minute to 14 days) | 345600 seconds (4 days) |
Key Takeaways
Set the retention period using the MessageRetentionPeriod attribute in seconds.
Allowed retention period range is from 60 seconds to 1209600 seconds (14 days).
Use AWS CLI, SDK, or Console to update the retention period on existing queues.
Ensure you have the correct queue URL and permissions before setting attributes.
MessageRetentionPeriod controls message lifetime, not message visibility.