0
0
AwsHow-ToBeginner · 3 min read

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 where MessageRetentionPeriod is 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=3600
Output
No output if successful; the command sets the retention period to 3600 seconds.
⚠️

Common Pitfalls

  • Setting MessageRetentionPeriod outside 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 VisibilityTimeout with 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

AttributeDescriptionAllowed ValuesDefault
MessageRetentionPeriodHow long messages stay in the queue before deletion60 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.