In DynamoDB, it's possible to "age-out" or delete items based on a time-to-live attribute. We cover how to set up such an attribute, what values you need to use, and how they can be used for both deletion of items as well as for aging older data out of dynamo into another table or even S3.
Instructor: [00:00] DynamoDB includes support for something called the Time to Live attribute. If you enable Time to Live on DynamoDB table, you can set a value that will control when a particular item will get deleted from the table. Notably, a Time to Live value cannot be an ISO date, it has to be a Unix timestamp in seconds.
[00:16] Also note that it's not guaranteed that an item with a Time to Live will get deleted immediately. If this is critically important for your application, you should use a filter expression on any of your queries to make sure that you don't return any value with a Time to Live that has expired but hasn't quite yet been deleted.
[00:32] If we take a dateTime and we increase the number in the minutes column by 1 and take that timestamp back to our testing table, we can add a new attribute with a number type called TTL. Note that the name of this attribute doesn't matter at all and can be any arbitrary value, but the value has to be a number because it has to be a Unix timestamp.
[00:50] We go to the overview, we can manage TTL. If we put in TTL as the attribute, we can run a preview. You can see that because the preview is before expiry date, we get no values returned.
[01:01] If we increase that value and run it again, we can see that the value with the Time to Live will get deleted. Note that no other values get deleted though because they don't have a Time to Live in them. The Time to Live attribute only executes if there's a value set on that attribute for that item.
[01:17] In this way, you can specify some items to get deleted and some items to stick around forever. If we refresh the console, you can see that there's a TTL on this attribute now. Note that when enabling TTL and looking at the enable TTL dialogue, we get the option to enable with a view type new and old images.
[01:33] We'll cover what this means deeper in the DynamoDB streams video but suffice it to say for now, that if we enable this, when items get deleted, they will get pushed to a DynamoDB stream which gives us 24 hours to process them and archive them perhaps to S3 if we want to.
[01:49] Note also that if you hover over the TTL value, it'll give you the UTC local time and region. This is highly useful because it's quite hard to read Unix timestamps.