How to use DynamoDB TTL to expire items automatically

Updated on
dynamodb self destruct banner

TTL is known as time to live. You can think of TTL as a ticking time bomb. You choose when you want the item to be deleted in the time (UNIX epoch) specified along with the item in the specified TTL attribute.

DynamoDB TTL use cases

  • Delete expired auth sessions automatically
  • Archive IoT sensor data from (ESP32, Greengrass)
  • Delete unverified users after 48 hours
  • Archive resumes of unqualified applicants from the past 6 months
  • Archive old data to S3 to save DB storage cost with TTL and DynamoDB streams for auditing purposes

How to enable TTL in DynamoDB table

  1. Navigate to you DynamoDB table and scroll all the way right till you see Additional settings.
  2. In the Additional settings you’ll find Time to Live (TTL) after scrolling down a bit.
  3. Click Turn on button

dynamodb additional settings ttl

In the TTL page put your attribute name which will be used as a TTL to delete the item at the specified time. e.g. expire_at

Click Turn on TTL

dynamodb ttl settings page

You will see TTL status On dynamodb ttl turned on status

How to test DynamoDB TTL?

Let’s create an item with TTL of 30 seconds later from current UNIX time.

Important

Make sure to use number (N) type to specify the TTL attribute.

To get the current UNIX epoch time in seconds you can use date +%s command.

dynamodb ttl create item example

  1. Go back to the Additional settings tab and click on Run preview in the Time to Live (TTL) section.
  2. Put the same attribute value which you did in the previous step, i.e. expire_at.
  3. In the Simulated date and time put the same or greater value of epoch time.
  4. Click Run preview

You will see your item in the able below. DynamoDB TTL simulated preview

After few minutes, if you go to the table and query for that item, it should no longer be there.

What is the DynamoDB TTL cost?

TTL has no additional cost. Thanks to AWS.

Is DynamoDB TTL reliable?

How accurate is DynamoDB TTL really?

Since the timestamp for TTL is stored in UNIX epoch time, it can only provide you granularity up to seconds, not milliseconds.

What about the accuracy of deletion?

The item doesn’t get deleted at the exact time. It gets deleted within a couple of hours in most cases, but it can take up to 48 hours.

Tip

If your DynamoDB TTL is not working, you should just wait for a while.

Is there any limit to DynamoDB TTL?

No there is no upper limit for DynamoDB TTL. You can set TTL of even more than 5 years.

How to trigger Lambda with DynamoDB TTL

  1. Go to your DynamoDB table settings and select Exports and streams tab
  2. In the DynamoDB stream details section click Turn on
  3. Now select Old image in the View type
  4. Click Turn on stream

Now you will receive updates for all the deleted items in Lambda function.

dynamodb stream settings for old image

Setup Lambda to capture the deleted items

You will need to create a Lambda function and add DynamoDB stream as event source.

Learn how to configure the Lambda to use this DynamoDB stream.

DynamoDB TTL alternatives

The best alternative right now is Amazon EventBridge Scheduler.

event bridge scheduler banner

You can create a one-time or repeating schedule which will trigger a Lambda function which can delete item(s) based on the specified time or whenever the schedule is triggered.


Hills 🏔 and Skills, What's Common?

They both need you to be on top.

You will get lifetime access with:

All yours, just at:

$149

Just type and your search result will magically appear here