Created
April 3, 2021 00:28
-
-
Save michaelrios/05dbf08efeb2efab86f12013bcb1129f to your computer and use it in GitHub Desktop.
Truncate a DynamoDB table for a given Partition Key prefix
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 | |
#### | |
#### Deletes all rows from a given TABLE_NAME with a given PK PREFIX | |
#### | |
#### This does do a FULL TABLE SCAN, and can be expensive | |
#### | |
#### It does require you have a package called jq for parsing json, but you can get that through brew, apt-get, etc… | |
#### | |
set -e | |
REGION=us-east-1 | |
TABLE_NAME=SomeTable | |
PK_KEY=PK | |
SK_KEY=SK | |
# this double quote sneaks in from the json response | |
PREFIX=\"SOME_PREFIX | |
# declare arrays to be filled later | |
declare -a pks | |
declare -a sks | |
# Get full table scan results and store them in a temp file | |
aws dynamodb scan --region ${REGION} --table-name $TABLE_NAME > "/tmp/dynamo_${TABLE_NAME}_scan.txt" | |
echo "got scan results with count..." | |
cat "/tmp/dynamo_${TABLE_NAME}_scan.txt" | jq ".Count" | |
# Get arrays of PartitionKeys and SortKeys | |
pks=(`cat "/tmp/dynamo_${TABLE_NAME}_scan.txt" | jq ".Items[].${PK_KEY}.S"`) | |
sks=(`cat "/tmp/dynamo_${TABLE_NAME}_scan.txt" | jq ".Items[].${SK_KEY}.S"`) | |
length=${#pks[@]} | |
# Delete from id list | |
for ((i=0; i<$length; i++));do | |
if [[ "${pks[$i]}" =~ ^${PREFIX} ]] | |
then | |
echo "$i: Deleting ${pks[$i]} from $TABLE_NAME..." | |
aws dynamodb delete-item --region us-east-1 --table-name $TABLE_NAME --key "{\"${PK_KEY}\": {\"S\": ${pks[$i]}}, \"${SK_KEY}\": {\"S\": ${sks[$i]}}}" | |
fi | |
done | |
# Remove id lists | |
rm "/tmp/dynamo_${TABLE_NAME}_scan.txt" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment