Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDb: Delete all items having same Hash Key

Tags:

Consider the following table:

Table (documentId : Hash Key, userId: Range Key) 

How can I write a code to delete all the items having the same documentId and preferably without retrieving the items.

like image 319
Hans Avatar asked Dec 14 '15 03:12

Hans


People also ask

How do I delete multiple items in DynamoDB?

To delete a single item, use DeleteItem. To delete multiple items, use BatchWriteItem. Despite the naming of BatchWriteItem , it can be used to put multiple items or to delete multiple items, and you can target one or more DynamoDB tables in the same API call.

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.

Is DynamoDB hash key unique?

"Hash and Range Primary Key" means that a single row in DynamoDB has a unique primary key made up of both the hash and the range key. For example with a hash key of X and range key of Y, your primary key is effectively XY.

Can DynamoDB have multiple hash keys?

Using normal DynamoDB operations you're allowed to query either only one hash key per request (using GetItem or Query operations) or all hash keys at once (using the Scan operation).


1 Answers

Currently, You cannot delete all the items just by passing the Hash key, to delete an item it requires Hash + Range because that's what makes it unique.

You have to know both your (hash + range) to delete the item.  

Edit: Here is the reference link from DynamoDB documentation http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_DeleteItem.html#API_DeleteItem_RequestSyntax

Please read the explanation of the "KEY" which clearly says that we must pass both Hash (Partition Key) and Range (Sort Key) to delete the item.

like image 91
Harshal Bulsara Avatar answered Oct 22 '22 19:10

Harshal Bulsara