10
votes

Does DynamoDB support TTL based on specific conditions? For example, I want the TTL to be enabled on records with a specific attribute and no TTL on records which does not have a value for that attribute.

The docs I've currently read to look into this issue are

  1. http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/howitworks-ttl.html
  2. http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/time-to-live-ttl-how-to.html

Both the docs indicate no specific information on that.

Thanks,

3

3 Answers

20
votes

Remember that DynamoDB is schemaless. All records do not have to have the same fields. So, only create the TTL field on the records which you wish to have expired. When the record is updated such that your "condition" is met, add the TTL field.

4
votes

TTL compares a non-empty filed (which has been set as TTL field) in DynamoDB with current timestamp, delete the item if its TTL field is earlier than current time stamp.

A typical way to use DynamoDB TTL, is to create a field (maybe name it as ExpirationTime) in you data object, add set it as (currentEpochTime + TimeToLive). After TimeToLive seconds, DynamoDB would find the value less than current EpochTime, and do the delete action (expect some delay).

Having said that, if you want to enable TTL after this table has been used by adding a new field to original data object. Existing data will not be affected as they do not have that field. You may want to run a script to add that new field value to all existing items to make TTL work for existing data.

0
votes

The information is not there, there is no such feature.

You can create a TTL on the record based on whatever condition you like to impose on a record. If it does not match those conditions, then don't create TTL or clean up existing TTL.

Records will get removed only if matches the TTL timestamp value or lower.

I'm not sure for a need for a build in feature.