Welcome to our blog, where we break down complex concepts into simple, easy-to-understand explanations. Today, we’ll discuss Time to Live (TTL) in Amazon DynamoDB. TTL allows you to delete items from your tables after a specified period automatically. This can be incredibly useful for managing data with a limited lifespan, such as session data, temporary files, or any other data that becomes irrelevant or outdated after a specific period.

How does TTL work in DynamoDB?

TTL allows you to set an expiration timestamp for each item in your table. This timestamp is stored as an attribute in the item, and when the current time surpasses the timestamp, DynamoDB automatically deletes the item. This process occurs in the background without any manual intervention or additional read/write capacity consumption.

Benefits of using TTL in DynamoDB

  1. Cost savings: By automatically deleting expired items, you can reduce the storage space required for your table, reducing your storage costs.

  2. Improved performance: With fewer items in your table, query and scan operations can be faster and more efficient.

  3. Simplified data management: TTL eliminates the need for manual data cleanup processes, reducing the complexity of managing your data.

  4. Compliance with data retention policies: TTL can help you comply with data retention policies by ensuring that data is automatically deleted after a specified period.

How to use TTL in DynamoDB?

  1. Enable TTL for the Table: You need to enable TTL on the table by specifying a TTL attribute. This attribute should be of type Number and store timestamps in epoch format.

  2. Setting TTL Values for Items:

    • When you insert or update an item in the table, you should set the TTL attribute to the epoch timestamp of when you want the item to be deleted.
    • For instance, if you want to delete an item 24 hours after its creation, you will set the TTL value to the current epoch time plus 86,400 seconds (24 hours). The TTL value must be set to a time not more than five years in the future. For example, 1171734022 (Feb 17, 2007) is too old and invalid.

Note: If the field is absent in an item, the TTL process ignores the item.

TTL: Monitoring

You can use the CloudWatch metrics tab to monitor TTL rates for a DynamoDB table. The following metrics are available:

TimeToLiveDeletedItemCount - This metric shows the number of items that have been deleted by TTL during the specified time period.

TTL: Nuances, Cautions, and Best Practices

  1. Deletion is Not Instantaneous:

    • TTL does not delete items immediately after they expire.
    • The deletion process can take a few days, and the duration varies based on the size and activity level of the table.
  2. Expired Items May Still Appear:

    • Even after items have expired, they can still be visible in reads, queries, and scans because they haven’t been deleted yet.
    • If your application requires not seeing the expired items in the result set, you will need to filter them out actively.
import boto3
import time

dynamodb = boto3.client('dynamodb')

# Get the current time in epoch format
current_time = int(time.time())

# Query the DynamoDB table and filter out expired items
response = dynamodb.query(
    KeyConditionExpression='pk = :pk_value',
    FilterExpression='expdate > :current_time',
        ':pk_value': {'S': primary_key},
        ':current_time': {'N': str(current_time)}
  1. Expired Items Can Be Updated:

    • If an item is past its expiration time but has yet to be deleted, it can still be updated.
    • Any successful update that alters or removes the expiration attribute will take effect.
  2. Deletion from Secondary Indexes:

    • When TTL deletes an item, it is removed from both local secondary indexes and global secondary indexes.
    • This removal is equivalent to a DeleteItem operation.
    • Notably, this operation is performed automatically and incurs no additional cost.


In conclusion, TTL in AWS DynamoDB is an essential feature for efficient and cost-effective database management. It automatically removes outdated data, keeping the database lean and reducing storage costs. However, careful configuration is crucial to avoid unintended data loss.

Be sure to check out more such insightful blogs in my Master DynamoDB: Demystifying AWS's Powerful NoSQL Database Service, One Byte at a Time series, for a deeper dive into DynamoDB's best practices and advanced features. Stay tuned and keep learning!