Use Soft Deletes in your DynamoDB tables

Serverless Tip #14: Use Soft Deletes in your DynamoDB tables

Imagine an application where users can create, read, update, and delete items through an API, with data stored in DynamoDB. Sometimes, your users accidentally delete items, and you get a growing number of support tickets for missing data.

You hunt through the logs to track down which item(s) the user deleted. Sadly, the log entry contained the item’s ID but not the full details. With the ID in hand, you restore a backup (you have those, right?) to get the missing item(s).

You can simplify the process by using “Soft Deletes.” Instead of deleting items on DELETE requests, add a “deletedAt” attribute with the current timestamp. Update your queries to filter out any records that contain the attribute.

Soft deletes give you an audit trail of deleted items. You can now find deleted items in your table and restore them by removing the deletedAt attribute.

If storage cost is a concern, you can enable TTL on your table to store deleted items only for a set period. For example, you can remove records 30 days after soft deletion.