Filtering expired items DynamoDB TTL implementation

Randini Senanayake
1 min readJan 28, 2022

Requirement :

Filter expired items from the DynamoDB using a AWS Lambda Function

Explanation :

Depending on the size and activity level of a table, the actual delete operation of an expired item can vary. TTL typically deletes expired items within 48 hours of expiration [1]. Items that have expired, but haven’t yet been deleted by the TTL background process, still appear in reads, queries, and scans.

To avoid the data being appeared in reads, queries, and scans we are able apply a filter expression to prevent them from appearing until they have been deleted by the TTL background process. The filter expression would only return items where the TTL expiration value is greater than the current time in epoch format. Documentation to read about filter expressions for queries [1] and scans [2].

Solution :

Implement the below code segment in your lambda function.

var epoch = Math.floor(Date.now() / 1000);
const queryTTLParams = {
TableName: table_name,

FilterExpression: "#theTTL > :ttl", //value comparison

ExpressionAttributeNames:{
"#theTTL": "expirationdate" //table column num format
},
ExpressionAttributeValues: {
":ttl": epoch //current datetime in epoch format
},
Limit: 1
}

References :

[1]https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/howitworks-ttl.html

[2]https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html#Query.FilterExpression

[3]https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Scan.html#Scan.FilterExpression

--

--