Skip to content

Instantly share code, notes, and snippets.

@lincolnthomas
Created September 1, 2020 18:38
Show Gist options
  • Save lincolnthomas/bcef2d942ea3949f8752b7340d6099c1 to your computer and use it in GitHub Desktop.
Save lincolnthomas/bcef2d942ea3949f8752b7340d6099c1 to your computer and use it in GitHub Desktop.
Empty a set of DynamoDB tables
It's a PITA to delete all items in an AWS DynamoDB table without deleting the table.
True, you can delete and re-create the table, but resource ARNs will change (like DyanmoDB Streams), which
can disrupt other resources depending on those ARNs.
I found https://www.npmjs.com/package/dynamodb-empty which does (most of) the job.
Note:
1. The README there is wrong, as of this writing. The correct syntax is:
$ dynamodb-empty --table {tablename}
(The --table is missing in the README)
2. If you don't supply --table, the usage text says you need to supply --name, which is also wrong.
3. It will not delete items from tables that also have a sort key. You can DuckDuckGo why that is,
and there's some code that people have written to do that too. I might copy dynamodb-empty and add that feature and
publish it myself at some point (will update this gist), as well as the group feature below.
Grouping Extension:
I added this to my ~/.bashrc to allow you to empty the contents of a similarly-named group of tables, e.g. "test-iris"
function _ddb-empty {
echo "Deleting all items for tables whose table name contains $1..."
for x in $(aws dynamodb list-tables | grep $1 | tr -d ',' | tr -d '"'); do dynamodb-empty --table $x; done
echo "Done."
}
alias ddb-empty=_ddb-empty
Usage:
$ ddb-empty {table-name-contains-string}
e.g.:
$ ddb-empty test-iris
Enjoy!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment