Deleting multiple records in dynamo db

Viewed 16932

can we delete multiple records in dynamo db only through range key?

I'm trying to do like this sql statement

delete from employee where employee_name='gopal';

so here the records with employee name gopal should get deleted

can we achieve same like this sql query in dynamo db?

4 Answers

You would have to do a BatchWriteItem to delete your items. From the documentation:

BatchWriteItem performs put and delete

Please Check This query it's working with multiple data

const params = {TableName: "test"};

docClient.scan(params, (error, result) => {
        if (error) { console.log(error,"error scan"); }

        result.Items.forEach(function(item) {          
            docClient.delete({Key:{mainID: item.id},TableName:"test"}, (error) => {
                if (error) {
                    console.log("Delete fail");
                }
                console.log("Delete  Success");
            });
        });

});    

Based on the API, there is no Batch Delete for DynamoDB.

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html

You need to query the item and delete with their partition key.

I would recommend looking at ODM,

https://github.com/clarkie/dynogels

to make the operations simpler.

Check out the sample calls for deletion,

Account.destroy('foo@example.com', function (err) {
  console.log('account deleted');
});

// Destroy model using hash and range key
BlogPost.destroy('foo@example.com', 'Hello World!', function (err) {
  console.log('post deleted')
});

BlogPost.destroy({email: 'foo@example.com', title: 'Another Post'}, function (err) {
  console.log('another post deleted')
});

@Kannaiyan thanks for your comments....

i found the solution with dynamoDB with java http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/JavaDocumentAPICRUDExample.html

DeleteItemSpec deleteItemSpec = new DeleteItemSpec().withPrimaryKey("Id", 120)
                .withConditionExpression("#ip = :val").withNameMap(new NameMap().with("#ip", "InPublication"))
                .**withValueMap(new ValueMap()**.withBoolean(":val", false)).withReturnValues(ReturnValue.ALL_OLD);

fetching the records with key, value pair and storing it in "withValueMap(new ValueMap()" and this helps me.

Thanks.

Related