How to Enable TTL in DynamoDB: Step-by-Step Guide
To enable
TTL in DynamoDB, specify a table and a timestamp attribute that stores the expiry time in Unix epoch format. Use the AWS Management Console, AWS CLI, or SDK to activate TimeToLiveSpecification with the attribute name, so expired items are automatically deleted.Syntax
Enabling TTL requires specifying the table name and the attribute that holds the expiration timestamp. The attribute must be a number representing Unix epoch time in seconds.
The key part is the TimeToLiveSpecification object with two properties:
Enabled: Set totrueto activate TTL.AttributeName: The name of the attribute storing the expiry timestamp.
bash
aws dynamodb update-time-to-live \
--table-name YourTableName \
--time-to-live-specification Enabled=true,AttributeName=ttlAttributeExample
This example uses AWS CLI to enable TTL on a table named Orders with the TTL attribute called expiryTime. The attribute should contain the expiration time as a Unix timestamp in seconds.
bash
aws dynamodb update-time-to-live \
--table-name Orders \
--time-to-live-specification Enabled=true,AttributeName=expiryTimeOutput
{
"TimeToLiveSpecification": {
"Enabled": true,
"AttributeName": "expiryTime"
}
}
Common Pitfalls
- Wrong attribute type: TTL attribute must be a number (Unix epoch time in seconds), not a string or date format.
- TTL attribute missing: Items without the TTL attribute are never deleted.
- TTL disabled: Forgetting to set
Enabled=truemeans TTL won't work. - Delay in deletion: TTL deletion is not immediate; it can take up to 48 hours after expiration.
bash
aws dynamodb update-time-to-live \
--table-name Orders \
--time-to-live-specification Enabled=false,AttributeName=expiryTime
# Correct way:
aws dynamodb update-time-to-live \
--table-name Orders \
--time-to-live-specification Enabled=true,AttributeName=expiryTimeQuick Reference
| Parameter | Description |
|---|---|
| TableName | Name of the DynamoDB table |
| Enabled | Set to true to activate TTL |
| AttributeName | Name of the attribute storing expiration time (Unix epoch seconds) |
| TTL Attribute Type | Must be Number (Unix epoch time in seconds) |
| Deletion Delay | Expired items may be deleted up to 48 hours later |
Key Takeaways
Enable TTL by specifying the expiration attribute and setting Enabled=true.
TTL attribute must be a number representing Unix epoch time in seconds.
Items without the TTL attribute are not deleted automatically.
TTL deletion is asynchronous and can take up to 48 hours after expiry.
Use AWS CLI, SDK, or Console to configure TTL on your DynamoDB table.