Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDb delete non-existent item does not fail, why?

I am deleting a non-existing record from the dynamodb table using dynamoDbMapper.delete(object) which uses default DynamoDBDeleteExpression

I was expecting some sort of exception to arise since the record is absent from the DB but it does nothing. It does not even have a return type which could tell if the delete was successful or failure. Is there a way to add a delete expression or something which will make my delete throw an exception if the item is absent from the db?

like image 237
Manthan Jamdagni Avatar asked Apr 17 '18 07:04

Manthan Jamdagni


People also ask

How do I delete items in DynamoDB?

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.

Which feature in DynamoDB allows you to automatically delete expired items from the table?

Amazon DynamoDB Time-to-Live (TTL) enables you to automatically delete expired items from your tables, at no additional cost.

How long does it take to delete a DynamoDB table?

Dynamo tables tend to be slow on deletes - around 5 minutes is quite normal. The problem is often people want to delete the table and re-create it as cleanup, but this will inject a downtime. Renaming also isn't fast - so deleting the items (and pay for that) is an option or to use TTLs.

Is DynamoDB fault tolerant?

DynamoDB automatically scales tables to adjust for capacity and maintains performance with zero administration. Availability and fault tolerance are built in, eliminating the need to architect your applications for these capabilities.


Video Answer


2 Answers

It is by design:

Unless you specify conditions, the DeleteItem is an idempotent operation; running it multiple times on the same item or attribute does not result in an error response.

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

like image 136
E.J. Brennan Avatar answered Oct 13 '22 21:10

E.J. Brennan


It's possible to utilize ReturnValues to determine if delete did anything or not. If ReturnValues.Attributes is empty, it means delete didn't find a record to delete, and you can throw an error in this case. Example in JavaScript:

async function deleteWithThrowIfNotExists() {
  const dynamo = new AWS.DynamoDB.DocumentClient();
  const parameters = {
    Key: {
      user: 'john'
    },
    ReturnValues: 'ALL_OLD',
    TableName: 'users'
  };
  const response = await dynamo.delete(parameters).promise();
  if (!response.Attributes) {
    throw new Error('Cannot delete item that does not exist')
  }
}
like image 8
Zhenya Avatar answered Oct 13 '22 20:10

Zhenya