Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all items in a DynamoDB table using bash with both partition and sort keys

I'm trying to delete all items in a DynamoDB table that has both partition and sort keys using AWS CLI in bash. The best thing I've found so far is:

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\"}}"

But this does not work with a table that has both the partition key and the sort key, and I have not yet been able to make it work with such a table. Any idea how to modify the script to make it work for a table with composite keys?

like image 699
svakili Avatar asked Aug 02 '18 18:08

svakili


People also ask

How do I delete all items in a DynamoDB table in AWS?

With the DynamoDB API, you use the DeleteItem action to delete data from a table, one item at a time. You must specify the item's primary key values. In addition to DeleteItem , Amazon DynamoDB supports a BatchWriteItem action for deleting multiple items at the same time.

Can we query DynamoDB with sort key?

You can not query only using a Sort Key. You need to specify a partition key to perform query operations. Else, you need to create a global secondary index or perform a scan operation.

Can we have two sort keys in DynamoDB?

Generally in DynamoDB you can create Local Secondary Indexes if you need alternative sort key: To give your application a choice of sort keys, you can create one or more local secondary indexes on an Amazon DynamoDB table and issue Query or Scan requests against these indexes.


1 Answers

I've created a node module to do this:

https://www.npmjs.com/package/dynamodb-empty

yarn global add dynamodb-empty
dynamodb-empty --table tableName
like image 70
Alan Cooney Avatar answered Sep 19 '22 14:09

Alan Cooney