Deleting Content in DynamoDB from the CLI

If you started developing serverless applications using AWS Lambda, chances are you also use DynamoDB as a storage backend.

If so, there are scenarios where you want to replace a table by an empty one:

  • Load & PerformanceTesting
  • Reset development/testing environment

There are basically 2 methods to do so with their own strengths and weaknesses.

Deletion/Creation

A quick and dirty way to do so is to delete and recreate the table. If you want to keep the settings you had before, you will need to extract a schema matching the --generate-cli-skeleton flag.

This can be achieved with

Once this is done, you can delete the table

And finally recreate it with

This is perfect (and fast!) when the table does not have any streams attached. However, you might have noticed that DynamoDB stream ARNs, which you will need to process streaming data, depend on their creation date-time as in

Which means that if you deleted the table and recreated it, you changed the Stream ARN, thus potentially lost the connection to your lambdas. Sub optimal.

Only deleting keys

If you need to preserve external connectivity, or for some reason cannot tolerate to delete and recreate the table, you might want to delete all its content.

To do that cleanly, you will first need to extract the schema of the table slightly differently that we did before:

Now you will need to scan your table for these keys, then use them to order deletion. The data extraction has a trick in the jq command:

This returns a single, long line of JSON objects of the table items separated by null characters. We just emulated the -print0 feature of find with jq and tr. .

Now we will need to pipe that in xargs via

The whole command being:

If you want to debug the code, you can add -t to the xargs options, which copies the output to stdout before the actual execution.

The advantage of this method is to maintain the overall coherence of your AWS environment. However, it is slooooooowwwwww, especially if you are in underpowered development environments.

Conclusion

This is quite a change compared to my previous K8s work, but I must say serverless seriously rocks, but there are a number of things I wish I had known before starting working on that technology.

This is just the first of a series of tips and tricks about Serverless on AWS so others don't have to spend the time I did on them. I hope you'll enjoy them!

Any question, feedback, don't hesitate to ask in the comments.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store