Delete large data with same partition key from DynamoDB

codereviewanskquestions picture codereviewanskquestions · Apr 6, 2018 · Viewed 9.5k times · Source

I have DynamoDB table structured like this

A   B    C    D
1   id1  foo hi
1   id2  var hello

A is the partition key and B is the sort key.

Let' say I only have the partition key and don't know the sort key and I'd like to delete all entries have the same partition key.

So I am thinking about loading entries by query with a fixed size (e.g 1000) and delete them in a batch until there are no more entries with the partition key left in DynamoDB.

Is it possible to delete entries without loading them first?

Answer

F_SO_K picture F_SO_K · Apr 6, 2018

https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_DeleteItem.html

DeleteItem

Deletes a single item in a table by primary key.

For the primary key, you must provide all of the attributes. For example, with a simple primary key, you only need to provide a value for the partition key. For a composite primary key, you must provide values for both the partition key and the sort key.

In order to delete an item you must provide the whole primary key (partition + sort key). So in your case you would need to query on the partition key, get all of the primary keys, then use those to delete each item. You can also use BatchWriteItem

https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchWriteItem.html

BatchWriteItem

The BatchWriteItem operation puts or deletes multiple items in one or more tables. A single call to BatchWriteItem can write up to 16 MB of data, which can comprise as many as 25 put or delete requests. Individual items to be written can be as large as 400 KB.

DeleteRequest - Perform a DeleteItem operation on the specified item. The item to be deleted is identified by a Key subelement: Key - A map of primary key attribute values that uniquely identify the item. Each entry in this map consists of an attribute name and an attribute value. For each primary key, you must provide all of the key attributes. For example, with a simple primary key, you only need to provide a value for the partition key. For a composite primary key, you must provide values for both the partition key and the sort key.