Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Databases and Redis

DynamoDB keeps items after their TTL time has passed

Your error

"TimeToLiveStatus": "ENABLED" but Scan still returns items whose expiry time has passed2 more lines it can show

Fix it fast

Most common cause, 1 of 4

The expiry is stored as a String, not a Number

Write the expiry as a Number that holds epoch seconds.

Fix
# AWS CLI with GNU date: expire 5 days from now
EXP=`date -d '+5 days' +%s`
aws dynamodb put-item --table-name YourTable --item '{"id": {"S": "123"}, "ttl": {"N": "'$EXP'"}}'

# boto3 resource API: a Python int is saved as a Number
from datetime import datetime, timedelta
table.put_item(Item={"id": "123", "ttl": int((datetime.now() + timedelta(days=5)).timestamp())})
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

DynamoDB TTL deletes an item only when the attribute named on the table, with matching case, holds a Number in Unix epoch seconds. Strings and millisecond values are ignored. Valid items can still linger for a few days before background deletion, so filter expired rows out of Query and Scan. Applies to DynamoDB with AWS CLI v2 and boto3 1.44.

  • Applies to Amazon DynamoDB TTL (standard and global tables), AWS CLI v2, boto3 1.44.9, AWS SDK for JavaScript v3 (@aws-sdk/client-dynamodb 3.1175)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Put three facts side by side: the attribute name the table watches, what one stale item really stores under that name, and the current epoch time. Most cases are settled by this alone.

Run this first
aws dynamodb describe-time-to-live --table-name YourTable
aws dynamodb get-item --table-name YourTable --key '{"id": {"S": "123"}}'
date +%s

Then find the line you see in this list and open its fix:

  1. 1The expiry is stored as a String, not a Numberget-item shows the attribute as {"S": "2026-11-20T10:00:00Z"} or {"S": "1763632800"} instead of an N value
  2. 2The Number is in milliseconds, or more than five years oldThe stored value has 13 digits, such as 1763632800000, or converts to a date years back
  3. 3Items use a different attribute name than the one TTL watchesdescribe-time-to-live reports AttributeName ttl while the items store TTL, expireAt or expiresAt
  4. 4TTL works, but deletion lags and reads still return the itemsThe value is a correct Number in seconds that passed only hours ago, and Query or Scan still lists the item

What does the error look like?

  • "TimeToLiveStatus": "ENABLED" but Scan still returns items whose expiry time has passed
  • Items with an expireAt or ttl date from last week are still in the table
  • TimeToLiveDeletedItemCount stays at 0 for the table

Cause 1 of 4

The expiry is stored as a String, not a Number

You seeget-item shows the attribute as {"S": "2026-11-20T10:00:00Z"} or {"S": "1763632800"} instead of an N value

Check

When DynamoDB TTL is enabled and nothing is ever deleted, check the type first. AWS states that the TTL process ignores items whose TTL attribute is not of the Number type, so an ISO date string, or even digits saved as text, will never expire.

Check
aws dynamodb get-item --table-name YourTable --key '{"id": {"S": "123"}}' --projection-expression "#t" --expression-attribute-names '{"#t": "ttl"}'

Fix

Write the expiry as a Number that holds epoch seconds. Items already in the table keep their old type until something rewrites them, so run a one-off update over the stale ones after fixing the writer.

Fix
# AWS CLI with GNU date: expire 5 days from now
EXP=`date -d '+5 days' +%s`
aws dynamodb put-item --table-name YourTable --item '{"id": {"S": "123"}, "ttl": {"N": "'$EXP'"}}'

# boto3 resource API: a Python int is saved as a Number
from datetime import datetime, timedelta
table.put_item(Item={"id": "123", "ttl": int((datetime.now() + timedelta(days=5)).timestamp())})

Verify

get-item now prints the attribute under N, and the console marks it with a TTL label.

Verify
aws dynamodb get-item --table-name YourTable --key '{"id": {"S": "123"}}'

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 2 of 4

The Number is in milliseconds, or more than five years old

You seeThe stored value has 13 digits, such as 1763632800000, or converts to a date years back

Check

A Number can still be skipped. DynamoDB expects epoch time at seconds granularity, ignores other formats, and only considers items whose TTL is no more than five years in the past. JavaScript's Date.now() returns milliseconds, so passing it straight through is the classic slip.

Check
date -d @1763632800

Fix

Divide by 1000 and round down before saving, which is how the AWS examples compute it. Correct the code that writes items, then rewrite the items that already carry a millisecond value.

Fix
// JavaScript: seconds, 5 days from now
const ttl = Math.floor((Date.now() + 5 * 24 * 60 * 60 * 1000) / 1000);

# Python: seconds, 5 days from now
from datetime import datetime, timedelta
ttl = int((datetime.now() + timedelta(days=5)).timestamp())

Verify

The stored value has 10 digits and date -d @<value> prints a date a few days ahead, not thousands of years away.

Verify
aws dynamodb get-item --table-name YourTable --key '{"id": {"S": "123"}}' --projection-expression "#t" --expression-attribute-names '{"#t": "ttl"}'

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 3 of 4

Items use a different attribute name than the one TTL watches

You seedescribe-time-to-live reports AttributeName ttl while the items store TTL, expireAt or expiresAt

Check

The name you give TTL is case sensitive and must be the exact attribute your reads and writes use. AWS warns that a mismatch leaves expired items undeleted. Renaming is not an edit in place: TTL has to be disabled and enabled again with the new name.

Check
aws dynamodb describe-time-to-live --table-name YourTable

Fix

Point the writers at the configured name, or switch TTL to the name the items already carry. UpdateTimeToLive returns ValidationException when TTL is already on with another name or when it was called for the table within the last hour, so disable first and wait an hour before enabling.

Fix
aws dynamodb update-time-to-live --table-name YourTable --time-to-live-specification "Enabled=false, AttributeName=ttl"
# about an hour later
aws dynamodb update-time-to-live --table-name YourTable --time-to-live-specification "Enabled=true, AttributeName=expireAt"

Verify

describe-time-to-live shows the name your items use with TimeToLiveStatus ENABLED.

Verify
aws dynamodb describe-time-to-live --table-name YourTable

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 4 of 4

TTL works, but deletion lags and reads still return the items

You seeThe value is a correct Number in seconds that passed only hours ago, and Query or Scan still lists the item

Check

Expiring and deleting are two steps. DynamoDB removes expired items in the background, typically within a few days, and until that happens they keep showing in reads and keep counting toward storage. Turning TTL on also takes about an hour to reach every partition.

Check
aws dynamodb describe-time-to-live --table-name YourTable

Fix

Treat TTL as cleanup, not as a hard cut-off. Give reads that must not see stale rows a filter on the TTL attribute against the current time.

Fix
aws dynamodb scan --table-name YourTable \
  --filter-expression "#t > :now" \
  --expression-attribute-names '{"#t": "ttl"}' \
  --expression-attribute-values '{":now": {"N": "'$(date +%s)'"}}'

Verify

Filtered reads skip the stale rows at once, and the table's TimeToLiveDeletedItemCount metric climbs over the following days.

Verify
aws cloudwatch get-metric-statistics --namespace AWS/DynamoDB \
  --metric-name TimeToLiveDeletedItemCount \
  --dimensions Name=TableName,Value=YourTable \
  --start-time 2026-11-20T00:00:00Z --end-time 2026-11-21T00:00:00Z \
  --period 3600 --statistics Sum

Confirmed by docs.aws.amazon.com

Did this fix it?

How do I stop it coming back?

  • Compute expiry in one shared helper that returns whole epoch seconds, with a unit test that rejects 13-digit values.
  • Keep the TTL attribute name in one constant used by both the table definition in your infrastructure code and the item writers.
  • Add the expiry filter to every Query and Scan that must never return stale data, rather than trusting deletion timing.
  • Turn TTL back on after restoring a table from backup; AWS says restored tables need it configured again.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: Dispatch Pro, stegnor.com, Raheeb.

Free tool for this

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.