0

I want to delete specific string matching items in the table. For example, Table1 is having Foo123Bar and Foo345Bar in the name column.

I want to delete two recs in name column.

Ashan
  • 18,898
  • 4
  • 47
  • 67
Nathon
  • 165
  • 1
  • 4
  • 13

2 Answers2

0

Here is how I solve it,

You need to scan the table with the conditions and delete them with a batch delete or delete item.

Scan and Query Example:

http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/dynamodb-example-query-scan.html

Batch Delete Example:

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#batchWriteItem-property

Kannaiyan
  • 12,554
  • 3
  • 44
  • 83
0

Based on whether the name column is primary key, sort key or an attribute, you can use different approaches to delete the item.

If the name column is a primary key, you can directly delete multiple items using BatchWriteItem. If you use DynamoDB Document Client, you can use the batchWrite method to delete multiple items in the table as shown below.

var params = {
  RequestItems: {
    'Table1': [
      {
        DeleteRequest: {
          Key: { name: 'Foo123Bar' }
        }
      },
      {
        DeleteRequest: {
          Key: { name: 'Foo345Bar' }
        }
      }
    ]
  }
};

var documentClient = new AWS.DynamoDB.DocumentClient();

documentClient.batchWrite(params, function(err, data) {
  if (err) console.log(err);
  else console.log(data);
});

If the name column is not a private key but a sort key or queryable key in an index, you need to query and delete the items by finding their primary key and sort key one by one using deleteItem method.

If the name column is a normal attribute, you need to scan and delete instead of query.

Using DynamoDB Document Client you can call the scan or query with required parameters easily.

Ashan
  • 18,898
  • 4
  • 47
  • 67
  • Thank you Ashan, but the name is not a key, I just want to delete the only item value, The above one is deleting the entire record. how to specify where condition please help on this – Nathon Sep 22 '17 at 15:56
  • Then you need to scan and conditionally match the name to find the record, after finding it, you can use the key to delete it. Or else you can create a global secondary index to quickly find the item and its key to delete. – Ashan Sep 22 '17 at 15:57