Skip to content

Instantly share code, notes, and snippets.

@BuonOmo
Last active September 8, 2023 15:07
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BuonOmo/f415466c4af38f236c11d879099af7cf to your computer and use it in GitHub Desktop.
Save BuonOmo/f415466c4af38f236c11d879099af7cf to your computer and use it in GitHub Desktop.
Delete all items in a dynamo db table
#!/usr/bin/env zsh
# I'm using jq to parse json. I really suggest using it.
# I'm using parallel for faster results, you could use xargs or a for loop if you don't have parallel.
main() (
set -eux
local options="--profile some-profile"
local key='id'
local table='table'
local keys_json=$(aws $options dynamodb scan --table-name $table --attributes-to-get $key | jq --compact-output '.Items[]')
# Parallel uses "\n" as a delimiter. So as long as none of you strings contains a "\n", you'll be ok.
[[ $#keys_json > 0 ]] && parallel "aws $options dynamodb delete-item --table-name $table --key" ::: "$keys_json"
)
main
@raulvc
Copy link

raulvc commented Jun 29, 2023

just a small suggestion if you have more than one option

local options=(--region us-east-1 --profile some-profile)
[...]
local keys_json=$(aws "${options[@]}" dynamodb scan --table-name $table --attributes-to-get $key | jq --compact-output '.Items[]')

@BuonOmo
Copy link
Author

BuonOmo commented Jun 29, 2023

@raulvc it looks like cool but not necessary, could you tell me why you use that ? (my string already has a space in it and it is working quite well afaik!)

@ic
Copy link

ic commented Sep 7, 2023

Thank you for sharing this script. Does it really delete all items, or just the first 1000 max (or 1MB worth of data)?

@BuonOmo
Copy link
Author

BuonOmo commented Sep 8, 2023

@ic I'm not sure woudl it would behave like you said ^^. It used to delete all, nowadays I don't know.

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