-
-
Save benweizhu/6fc3c9b2436d254af2954aefb1318028 to your computer and use it in GitHub Desktop.
Truncate all keys in a dynamodb table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment