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.
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.
Practice
(1/5)
1. What does TTL (Time To Live) do in DynamoDB?
easy
A. Encrypts data stored in the table.
B. Backs up data periodically.
C. Automatically deletes items after their expiration time.
D. Indexes data for faster queries.
Solution
Step 1: Understand TTL purpose
TTL is designed to remove expired data automatically to save storage and keep data fresh.
Step 2: Identify TTL behavior
It deletes items after their expiration timestamp passes, not encrypting or backing up data.
Final Answer:
Automatically deletes items after their expiration time. -> Option C
Quick Check:
TTL = automatic deletion [OK]
Hint: TTL means automatic deletion after expiry [OK]
Common Mistakes:
Thinking TTL encrypts data
Confusing TTL with backup
Assuming TTL speeds up queries
2. Which attribute type must the TTL attribute have in DynamoDB?
easy
A. String representing a date in ISO format
B. Number representing Unix epoch time in seconds
C. Boolean indicating if item is expired
D. List of timestamps
Solution
Step 1: Recall TTL attribute format
The TTL attribute must be a number storing the expiration time as Unix epoch seconds.
Step 2: Eliminate other types
Strings, booleans, or lists are not valid for TTL expiration timestamps.
Final Answer:
Number representing Unix epoch time in seconds -> Option B
Quick Check:
TTL attribute = Unix epoch seconds [OK]
Hint: TTL attribute stores expiration as Unix epoch seconds [OK]
Common Mistakes:
Using string dates instead of numbers
Using boolean instead of timestamp
Trying to store multiple timestamps
3. If an item has a TTL attribute set to 1650000000 (Unix epoch seconds), when will DynamoDB delete it?
medium
A. Within 48 hours after 1650000000 seconds
B. Exactly at 1650000000 seconds
C. Immediately after setting the TTL attribute
D. Only when manually deleted
Solution
Step 1: Understand TTL deletion timing
DynamoDB deletes expired items approximately after the TTL timestamp, not exactly at that second.
Step 2: Recall deletion delay
Deletion can take up to 48 hours after the expiration time to occur.
Final Answer:
Within 48 hours after 1650000000 seconds -> Option A
Quick Check:
TTL deletion delay = up to 48 hours [OK]
Hint: TTL deletion can be delayed up to 48 hours [OK]
Common Mistakes:
Expecting deletion exactly at expiration time
Thinking deletion is immediate
Assuming manual deletion is required
4. You set TTL on an attribute but notice expired items are not deleted after 3 days. What is a likely cause?
medium
A. TTL attribute is not a number with Unix epoch seconds
B. TTL is enabled but the table is in a different AWS region
C. You forgot to enable encryption on the table
D. The table has no primary key
Solution
Step 1: Check TTL attribute format
If the TTL attribute is not a number with Unix epoch seconds, DynamoDB ignores it for expiration.
Step 2: Verify other options
Region difference, encryption, or primary key absence do not prevent TTL deletion.
Final Answer:
TTL attribute is not a number with Unix epoch seconds -> Option A
Quick Check:
TTL attribute format error blocks deletion [OK]
Hint: TTL attribute must be Unix epoch number [OK]
Common Mistakes:
Assuming region affects TTL
Thinking encryption affects TTL
Ignoring TTL attribute data type
5. You want to ensure items expire exactly after 24 hours using TTL. Which approach is best considering DynamoDB's TTL behavior?
hard
A. Disable TTL and run a scheduled job to delete expired items
B. Set TTL attribute to current time + 86400 seconds and manually delete items after 24 hours
C. Use a string date format for TTL attribute for precise timing
D. Set TTL attribute to current time + 86400 seconds and accept up to 48-hour deletion delay
Solution
Step 1: Understand TTL timing limits
DynamoDB TTL deletes items approximately after expiration, with up to 48 hours delay.
Step 2: Choose best practical approach
Setting TTL attribute to current time + 86400 seconds (24 hours) is correct; manual deletion adds complexity and string format is invalid.
Step 3: Consider alternatives
Disabling TTL and scheduling deletes is possible but less efficient than TTL's automatic deletion.
Final Answer:
Set TTL attribute to current time + 86400 seconds and accept up to 48-hour deletion delay -> Option D
Quick Check:
TTL delay means accept approximate expiry [OK]
Hint: TTL deletion timing is approximate, plan accordingly [OK]
Common Mistakes:
Expecting exact deletion timing
Using string instead of number for TTL
Disabling TTL unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.