0
0
DynamoDBquery~5 mins

TTL behavior and timing in DynamoDB

Choose your learning style9 modes available
Introduction

TTL helps automatically remove old data from your database so it stays clean and fast.

You want to delete expired user sessions automatically.
You need to remove outdated logs after a certain time.
You want to keep only recent data and delete old records without manual work.
You want to save storage costs by deleting unused data.
You want to ensure data privacy by removing data after a set time.
Syntax
DynamoDB
TTL attribute: A table attribute that stores the expiration time as a Unix timestamp (seconds since Jan 1, 1970).
Enable TTL on the table and specify the attribute name.
DynamoDB automatically deletes items after the timestamp passes.
The TTL attribute must be a number representing the expiration time in Unix epoch seconds.
Deletion by TTL is not immediate; it usually happens within 48 hours after expiration.
Examples
This item will be deleted after the Unix time 1700000000 passes.
DynamoDB
TTL attribute name: "expireAt"
Item example: { "id": "123", "expireAt": 1700000000 }
All items with 'ttlTimestamp' in the future will be kept; expired ones will be deleted automatically.
DynamoDB
Enable TTL on table 'Users' with attribute 'ttlTimestamp'
Sample Program

This creates a table with a TTL attribute. The user 'Alice' will be deleted automatically about 1 hour after insertion.

DynamoDB
CREATE TABLE Users (
  userId STRING HASH KEY,
  name STRING,
  ttlTimestamp NUMBER
);

-- Enable TTL on 'ttlTimestamp' attribute via AWS Console or CLI

-- Insert a user with TTL 1 hour from now
INSERT INTO Users (userId, name, ttlTimestamp) VALUES ('u1', 'Alice', UNIX_TIMESTAMP() + 3600);
OutputSuccess
Important Notes

TTL deletion is handled by DynamoDB internally and can take up to 48 hours after expiration.

Expired items are not immediately removed, so your application should handle expired data if needed.

TTL only deletes items; it does not update or modify them.

Summary

TTL automatically deletes expired items based on a timestamp attribute.

Expiration time is stored as Unix epoch seconds in a specific attribute.

Deletion timing is approximate and can take up to 48 hours after expiration.