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.
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.
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
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.