Skip to content

Instantly share code, notes, and snippets.

@toshke
Forked from k3karthic/truncate_dynamodb.sh
Last active June 15, 2023 13:08
Show Gist options
  • Save toshke/d972b56c6273639ace5f62361e1ffac1 to your computer and use it in GitHub Desktop.
Save toshke/d972b56c6273639ace5f62361e1ffac1 to your computer and use it in GitHub Desktop.
Truncate all keys in a dynamodb table
#!/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"
@conraddf
Copy link

conraddf commented May 4, 2018

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

@ShashwatRastogi-Reflektion

Thank you :)

@howdyhyber
Copy link

Does this supports truncating with secondary index?

@mrsinham
Copy link

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)

@michaelrios
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment