11

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" );
    }
  });
Max
  • 1,054
  • 1
  • 12
  • 20
Dhaval Mojidra
  • 194
  • 1
  • 2
  • 11
  • Looking for this? https://stackoverflow.com/questions/46335395/deleting-multiple-records-in-dynamo-db – skott Nov 02 '19 at 11:05
  • 1
    Does one of the posted answers resolve your question? Please accept, if so. It will help future readers in search of an answer. – jarmod May 24 '22 at 12:26

2 Answers2

23

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));
})();
jarmod
  • 71,565
  • 16
  • 115
  • 122
3

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.

Itay Maman
  • 30,277
  • 10
  • 88
  • 118