Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete multiple rows in DynamoDB?

I am attempting to delete data from a DynamoDB table.

If I delete data using a partition key, it works.

But when I delete multiple rows using any other fields, it fails.

var params = {
  TableName: "test",
  Key: {
    dmac: dmac,
  },
  ConditionExpression: "dmac= :dmac"
};

docClient.delete( params, (error) => {
  if (error) {
    console.log( "Delete data fail" );
  } else { 
    console.log( "Delete data Success" );
  }
});
like image 530
Dhaval Mojidra Avatar asked Dec 29 '25 00:12

Dhaval Mojidra


2 Answers

Items (or rows) in DynamoDB are uniquely identified by their primary key. A table can have a simple primary key (a partition key) or a composite primary key (a partition key plus a sort key).

To delete an item, you must provide the full primary key (whether it's a simple partition key or composite partition key plus sort key).

So, if you want to delete items that meet a specific condition, for example cars with maxspeed < 120, then issue a query or scan to identify those items, retrieve the primary keys, and then delete the items in a second operation.

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.

Here is an AWS SDK v2 example of deleting multiple items:

const aws = require("aws-sdk");
const ddb = new aws.DynamoDB({ region: "us-east-1" });

(async () => {
  const params = {
    RequestItems: {
      albums: []
    }
  };

  params.RequestItems.albums.push({
    DeleteRequest: {
      Key: {
        pk: { S: "The Who" },
        sk: { S: "Tommy" }
      }
    }
  });

  params.RequestItems.albums.push({
    DeleteRequest: {
      Key: {
        pk: { S: "The Beatles" },
        sk: { S: "Abbey Road" }
      }
    }
  });

  await ddb.batchWriteItem(params).promise();
})();

Here is an AWS SDK v3 example of deleting multiple items:

const {
  BatchWriteItemCommand,
  DynamoDBClient
} = require("@aws-sdk/client-dynamodb");
  
(async () => {
  const client = new DynamoDBClient({ region: "us-east-1" });

  const params = {
    RequestItems: {
      albums: []
    }
  };

  params.RequestItems.albums.push({
    DeleteRequest: {
      Key: {
        pk: { S: "The Who" },
        sk: { S: "Tommy" }
      }
    }
  });

  params.RequestItems.albums.push({
    DeleteRequest: {
      Key: {
        pk: { S: "The Beatles" },
        sk: { S: "Abbey Road" }
      }
    }
  });

  await client.send(new BatchWriteItemCommand(params));
})();
like image 56
jarmod Avatar answered Dec 31 '25 16:12

jarmod


in DynamoDB you can only delete an item using its key (that is: the partition key and the sort key, if it is defined on the table). This is, for example, underlined by the fact that the Key attribute is a required attribute in the canonical specification of the delete operation. See: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_DeleteItem.html#DDB-DeleteItem-request-Key

This means that if you want to delete an item using other attributes you must first lookup the item by the attributes you do have, extract the key from the returned item, and then delete the item using that key.

The standard solution for "looking up an item by attributes that are not the item's key" is to define a global secondary index (GSI) on the table with those attribute(s) defined as the GSI's key.

like image 42
Itay Maman Avatar answered Dec 31 '25 17:12

Itay Maman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!