0
0
DynamodbHow-ToBeginner · 4 min read

How to Set TTL on Item in DynamoDB: Simple Guide

To set TTL on an item in DynamoDB, add a numeric attribute representing the expiration time as a Unix epoch timestamp in seconds, and enable TTL on the table specifying this attribute. DynamoDB will automatically delete items after the timestamp passes using the TimeToLiveSpecification feature.
📐

Syntax

To enable TTL on a DynamoDB table, you specify the TTL attribute name and enable the feature using the UpdateTimeToLive API. Each item must have this attribute with a Unix epoch time (in seconds) indicating when the item expires.

  • TTL attribute: A number attribute holding the expiration timestamp.
  • Enable TTL: Use TimeToLiveSpecification with Enabled set to true and specify the attribute name.
bash
aws dynamodb update-time-to-live --table-name YourTableName --time-to-live-specification Enabled=true,AttributeName=ttl
Output
An output confirming TTL is enabled on the table with the specified attribute.
💻

Example

This example shows how to add an item with a TTL attribute named ttl set to expire 1 hour from now, and how to enable TTL on the table.

python
import boto3
import time

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('YourTableName')

# Set TTL to 1 hour from current time
ttl_timestamp = int(time.time()) + 3600

# Put item with TTL attribute
response = table.put_item(
    Item={
        'PrimaryKey': 'item1',
        'Data': 'Sample data',
        'ttl': ttl_timestamp
    }
)

print('PutItem succeeded:', response)

# Enable TTL on the table (run once, usually via AWS CLI or console)
client = boto3.client('dynamodb')
client.update_time_to_live(
    TableName='YourTableName',
    TimeToLiveSpecification={
        'Enabled': True,
        'AttributeName': 'ttl'
    }
)
Output
PutItem succeeded: {'ResponseMetadata': {'RequestId': '...', 'HTTPStatusCode': 200, ...}}
⚠️

Common Pitfalls

  • Not enabling TTL on the table after adding the TTL attribute to items.
  • Using a TTL attribute with a non-numeric or incorrect timestamp format.
  • Expecting immediate deletion; DynamoDB deletes expired items typically within 48 hours.
  • Using TTL attribute names that conflict with reserved keywords.
python
## Wrong: TTL attribute as string instead of number
response = table.put_item(
    Item={
        'PrimaryKey': 'item2',
        'Data': 'Wrong TTL format',
        'ttl': '2024-06-01T12:00:00Z'  # Incorrect format
    }
)

## Right: TTL attribute as Unix epoch number
import time
response = table.put_item(
    Item={
        'PrimaryKey': 'item2',
        'Data': 'Correct TTL format',
        'ttl': int(time.time()) + 3600
    }
)
📊

Quick Reference

Summary tips for setting TTL in DynamoDB:

  • TTL attribute must be a number representing Unix epoch time in seconds.
  • Enable TTL on the table via AWS CLI, SDK, or Console before expecting automatic deletion.
  • Expired items are deleted asynchronously and may remain briefly after expiration.
  • Use a consistent attribute name for TTL across all items.

Key Takeaways

Set a numeric TTL attribute with Unix epoch time in seconds on each item to expire.
Enable TTL on your DynamoDB table specifying the TTL attribute name.
DynamoDB deletes expired items asynchronously, usually within 48 hours.
Use correct numeric timestamp format; string dates will not work.
TTL helps automatically clean up expired data and reduce storage costs.