TTL (Time To Live) helps automatically delete old data from your database after a set time. This keeps your database clean and saves space.
0
0
TTL attribute setup in DynamoDB
Introduction
You want to remove expired user sessions automatically.
You need to delete temporary files or logs after a few days.
You want to keep only recent data and remove old records without manual cleanup.
You want to save storage costs by deleting outdated items.
You want to manage data lifecycle without writing extra code.
Syntax
DynamoDB
1. Choose a table in DynamoDB. 2. Pick an attribute (column) to store the expiration time as a Unix timestamp (seconds since 1970-01-01T00:00:00Z). 3. Enable TTL on the table and specify the attribute name. 4. DynamoDB will delete items automatically after the timestamp passes.
The TTL attribute must be a number representing the expiration time in Unix epoch seconds.
DynamoDB deletes expired items within 48 hours after expiration, but not immediately.
Examples
This means the item will expire at the Unix time 1700000000 seconds.
DynamoDB
TTL attribute name: "expireAt" Value example: 1700000000
This sets up TTL on the table using the chosen attribute.
DynamoDB
Enable TTL using AWS Console: - Go to DynamoDB table - Select "Time to Live" - Enter attribute name "expireAt" - Click Enable
Sample Program
Here we add an item with an expiration time. DynamoDB will remove it after that time.
DynamoDB
PUT item with TTL attribute: { "UserId": "123", "Name": "Alice", "expireAt": 1700000000 } This item will be deleted after the Unix time 1700000000.
OutputSuccess
Important Notes
TTL only works on one attribute per table.
Expired items are deleted automatically but may still appear briefly after expiration.
You cannot recover items deleted by TTL.
Summary
TTL helps auto-delete old data using a timestamp attribute.
Set TTL attribute as Unix epoch seconds and enable TTL on the table.
DynamoDB cleans expired items automatically to save space and cost.