Skip to content

Instantly share code, notes, and snippets.

@pushplay
Last active January 4, 2023 14:14
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save pushplay/d2cac7ca1a10a5a49f6947a02657a23a to your computer and use it in GitHub Desktop.
Save pushplay/d2cac7ca1a10a5a49f6947a02657a23a to your computer and use it in GitHub Desktop.
Delete all items (clear) in a DynamoDB table using bash
#!/bin/bash
TABLE_NAME=TableName
KEY=id
aws dynamodb scan --table-name $TABLE_NAME --attributes-to-get "$KEY" --query "Items[].id.S" --output text | tr "\t" "\n" | xargs -t -I keyvalue aws dynamodb delete-item --table-name $TABLE_NAME --key '{"id": {"S": "keyvalue"}}'
@duo-xu
Copy link

duo-xu commented Mar 16, 2018

I noticed that the "KEY=id" is not used and it looks like "id" in aws cli commande is hardcoded 😃

@crccheck
Copy link

yeah, here's an alternate version:

aws dynamodb scan --table-name $TABLE_NAME --attributes-to-get "$KEY" \
  --query "Items[].$KEY.S" --output text | \
  tr "\t" "\n" | \
  xargs -t -I keyvalue aws dynamodb delete-item --table-name $TABLE_NAME \
  --key "{\"$KEY\": {\"S\": \"keyvalue\"}}"

@alez007
Copy link

alez007 commented Apr 28, 2018

what if the result has more than 1MB is size ?

@chetanmelkani
Copy link

I think it won't work when the table has both the hask key and the sort key.

@sarnobat
Copy link

sarnobat commented Aug 1, 2018

You also need --region in both aws subcommands (in my build, anyway).

@sarnobat
Copy link

sarnobat commented Aug 1, 2018

I wonder if there's a faster way of doing this. While it's often not necessary to optimize code, this particular one could do with something quicker. I guess you could drop and recreate your table (using the json out of aws describe-table).

@lqueryvg
Copy link

@chetanmelkani is correct - this won't work if there is also a sort key

@OtterFlip
Copy link

OtterFlip commented Sep 28, 2018

@lqueryvg - Good point.

Here's a modified version of the above command line which will scan a dynamo table and delete each item in the table. It works for tables which have both a partition and a sort key.

This command uses an AWS CLI profile named "admin" so change it to whichever profile name works for you
This command assumes the partition and sort keys are both of Dynamo type "S" (string) and so alter the attribute types as needed
Change the values of these three enviornment variables to match your table name as well as the names of its partition and sort keys/columns/attributes

export DYNAMO_TABLE="MyTableName"
export DYN_HASH_KEY="MyPartitionKeyName"
export DYN_SORT_KEY="MySortKeyName"

In one command, delete all items in a dynamo table which has both partition and sort keys

aws --profile admin dynamodb scan --table-name ${DYNAMO_TABLE} --attributes-to-get "${DYN_HASH_KEY}" "${DYN_SORT_KEY}" --output text | awk -v DYNAMO_TABLE="$DYNAMO_TABLE" -v DYN_HASH_KEY="$DYN_HASH_KEY" -v DYN_SORT_KEY="$DYN_SORT_KEY" '/PK_HASH/{ HASH=$2; next } /PK_SORT/{ print "aws --profile admin dynamodb delete-item --table-name " DYNAMO_TABLE " --key "{\"" DYN_HASH_KEY "\": {\"S\": \"" HASH "\"},\"" DYN_SORT_KEY "\": {\"S\": \"" $2 "\"}}"\0" }' | xargs -t -0 bash -c

UPDATE: I just noticed that there's still a hard-coded attribute/column name that's not using DYN_HASH_KEY and DYN_SORT_KEY. My table's partition/hash and sort keys are named "PK_HASH" and "PK_SORT" - and you can see they're hard-coded in the above command. So it won't actually work for you unless you modify PK_HASH and PK_SORT to match your own table's hash and sort key names. I'll see if I can fix this, but I'm having difficulty with awk's conditional expressions accepting the awk environment variables in the comparison expression.

@shapan1
Copy link

shapan1 commented Nov 24, 2021

Is there a way I can delete all items with different sort keys but the same PK?

@pushplay
Copy link
Author

That should be doable. Starting from @OtterFlip's work you'll want to replace the "dynamodb scan" with "dynamodb query" and add a "--key-condition-expression" to specify the value of your PK. Check out the docs for details on that.

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