-
-
Save toshke/d972b56c6273639ace5f62361e1ffac1 to your computer and use it in GitHub Desktop.
| #!/bin/bash | |
| #### | |
| #### Delete (remove) all items from Aws Dynamo DB table, by specifing table name and primary key | |
| #### | |
| #### Forked from https://gist.github.com/k3karthic/4bc929885eef40dbe010 | |
| #### | |
| #### Usage: | |
| #### clean-dynamo-table TABLE_NAME PRIMARY_KEY | |
| #### | |
| set -e | |
| TABLE_NAME=$1 | |
| KEY_NAME=$2 | |
| # Get id list | |
| aws dynamodb scan --table-name $TABLE_NAME | jq ".Items[].$KEY_NAME.S" > "/tmp/dynamo_${TABLE_NAME}_keys.txt" | |
| ALL_KEYS=$(cat "/tmp/dynamo_${TABLE_NAME}_keys.txt") | |
| # Delete from id list | |
| for key in $ALL_KEYS;do | |
| echo "Deleting $key from $TABLE_NAME..." | |
| aws dynamodb delete-item --table-name $TABLE_NAME --key "{ \"$KEY_NAME\": { \"S\": $key }}" | |
| done | |
| # Remove id list | |
| rm "/tmp/dynamo_${TABLE_NAME}_keys.txt" |
Great, thanks!
Thanks for this.
You should add this --select SPECIFIC_ATTRIBUTES --attributes-to-get $KEY_NAME to the scan line for better performance and reduced read capacity usage
Thank you :)
Does this supports truncating with secondary index?
truncating with secondary index :
aws dynamodb scan --table-name MyTable --region eu-west-1 | jq -r '.Items[] | "aws dynamodb delete-item --region eu-west-1 --table-name MyTable --key \"{\\\"HashKey\\\":{\\\"S\\\":\\\"\(.Id.S)\\\"},\\\"SortKey\\\":{\\\"S\\\":\\\"\(.SecondId.S)\\\"}}\""' | source /dev/stdin
(slow)
Thank you for making this and sharing! I built on yours to be able to delete only rows with a given Partition Key prefix and Sort Key
I think this would help @howdyhyber
https://gist.github.com/michaelrios/05dbf08efeb2efab86f12013bcb1129f
Works perfectly. Thank you.