3

We want to run a query, in which all the items that are returned, are deleted. More clearly, what we want to do exactly is run a query, in which if an item matches the condition, it should be included in the response, and be deleted from Amazon DynamoDB. And then the query should go with the second option.

So, after the query would respond, there would no such orders exist in database, since they were deleted on the go.


An example workflow with 5 items (items sample img. below) would look like -

enter image description here


  • A Query runs checking if From = Kartik.
  • The query comes on 1st item (1000) & finds that it matches the condition.
  • It captures the item, and deletes it from the Table. Now, only the response contains this item, not the table.
  • The query moves onto further items (1001 & 1002) and finds that they don't fit under the condition, so it doesn't even capture them, and does not delete too.
  • The query finds the 4th item (1003) matching the condition. So, it captures it in the response, and deletes it from the table.
  • Same as above for the 5th item (1004).

Now, the query completes, and returns a response containing ONLY the 1st, 4th & 5th Item. Now if I go and look for them in DynamoDB, it would return an error because they were deleted from there.


So, that's how I want the flow to be. Any chances of this being possible to do?

Any help is appreciated! Thanks!

smac2020
  • 9,637
  • 4
  • 24
  • 38
kartik
  • 550
  • 1
  • 6
  • 19

3 Answers3

6

You can perform a deleteItem operation for an item, and get its old value (before delete) by setting: "ReturnValues": "ALL_OLD" in the request params.

To delete an item you must specify its primary key. so you can only delete one item. (in your case From doesn't seem to be the primary key)

DeleteItem doc

You can perform a delete within a batchWriteItem operation to deal with multiple items at once. But note that batchWriteItem is not atomic i.e some delete ops may fail, and you can find them in batchWriteItem's response.

BatchItem doc

reda la
  • 801
  • 1
  • 6
  • 10
1

Two days to delete Item

1) Enable DynamoDB Streams and perform additional activities using lambda https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Streams.html

2) Use DeleteItem or BatchWriteItem api methods to perform search and deletion. https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/SQLtoNoSQL.DeleteData.html#SQLtoNoSQL.DeleteData.DynamoDB

In both of the cases, it requires to find the item by PartitionKey , PartitionKey+Range sort key.

OrderID is not suitable primary key, since you want to find item by From Column. Also, Scan is not option here to do that.

So to do Query on those items, There are ways to make it efficient:

1) suggest to make FROM as partition key and OrderID as sort/range key (Composite Key)

FROM | OrderDetails

kartik | kartik#1000

kartik | kartik#1003

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

How can I Use begins_with method on primary key in DynamoDB?

https://aws.amazon.com/blogs/database/using-sort-keys-to-organize-data-in-amazon-dynamodb/

Then you can use KeyConditionExpression operators like begins_with on Sort key and EQ operator on Partition key and get all records.

1

As an additional detail to reda la's answer, as doc mentions:

The individual PutItem and DeleteItem operations specified in BatchWriteItem are atomic; however BatchWriteItem as a whole is not.

So if your only will is to delete multiple items in dynamodb, you can do it with BatchWriteItem atomically.

mozh
  • 33
  • 4