Skip to content

Instantly share code, notes, and snippets.

@sumanmaity112
Created February 7, 2023 23:17
Show Gist options
  • Save sumanmaity112/342c7824f8450a3654adac6b6bb725fd to your computer and use it in GitHub Desktop.
Save sumanmaity112/342c7824f8450a3654adac6b6bb725fd to your computer and use it in GitHub Desktop.
Delete all data from dynamo table without deleting the actual table
#!/bin/bash
# jq-1.6, aws-cli/2.9.19
# shellcheck disable=SC2086
_scan_table() {
local total_segments="$1"
local segment="$2"
shift || true
shift || true
local table_name="$1"
local keys=( "${@:2}" )
aws dynamodb scan \
--table-name "${table_name}" \
--attributes-to-get ${keys[*]} \
--total-segments "${total_segments}" \
--segment "${segment}"
}
_purge_table() {
local total_segments="$1"
local segment="$2"
local table_name="$3"
local file_name="__chunk_${segment}.json"
local chunks
chunks=$(_scan_table "$@" \
| jq -r ".Items[] | tojson" \
| awk '{ print "{\"DeleteRequest\": {\"Key\": " $0 "}}" }' \
| jq -sc "_nwise(25)" \
| awk -v table_name="${table_name}" '{ print "{\"" table_name "\":" $0 "}" }')
for chunk in ${chunks} ; do
local chunk_size=$(echo "$chunk" | jq ".${table_name} | length")
if [ "${chunk_size}" -ne 0 ]; then
echo "Deleting ${chunk_size} elements"
aws dynamodb batch-write-item \
--request-items "file://${file_name}" | jq
else
echo "${table_name} table is already empty"
fi
rm -rf "${file_name}"
done
}
export -f _scan_table
export -f _purge_table
total_segments=${1}
shift || true
for segment in $(seq 0 $((total_segments-1)))
do
nohup bash -c "_purge_table ${total_segments} ${segment} $*" &
done
# sh purge-dynamo-table.sh <total segments> <table name> <key attributes>
# sh purge-dynamo-table.sh 5 test id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment