1
votes

I have a DynamoDB table with one attribute as a primary key ItemName.

Every attribute that is added to the table is named using the current UTC timestamp.

I want to do a conditional deletion of the item when I delete the last attribute which will check if there's another attribute other than ItemName and if there isn't I want to delete the item.

Is there anyway to do so?

Thanks.

1

1 Answers

2
votes

As far as I know, there isn't a way to tell DynamoDB to automatically do this for you. They will create a new item for you if you use the UpdateItem method to to add an attribute to an item that doesn't already exist, but they won't automatically delete the item when all attributes (except for the key) have been deleted.

The best way to do this is by using the ReturnValues attribute of the UpdateItem method. Set it to ALL_NEW to get the current state of the row returned back to you. Your code can then check if there are any additional attributes on the item and call the DeleteItem method, as appropriate.

EDIT: Further, you can add a ConditionExpression to the DeleteItem method call to check for the existence of an attribute before you delete it.