amazon-web-servicesamazon-dynamodb

Deleting last element in a DynamoDB string set


Say I have a DynamoDB document with a String Set attribute with one string element in it. If I delete the element using the DELETE update expression, the set will be empty. But DynamoDB doesn't allow empty set attributes, per the docs,

An attribute value cannot be an empty set (string set, number set, or binary set), however, empty lists and maps are allowed. https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html

What is the behavior of the update expression in this case, and is it documented anywhere? I haven't been able to find any references to whether this would be an error or if Dynamo would just delete the attribute on the document since the set is empty anyway.


Solution

  • If the element you're removing using theDELETE update expression is the only one in the set, the entire attribute is deleted.

    Similarly, the ADD update expression creates a set if it doesn't exist and then inserts the element.

    This behavior does not seem to be clearly described anywhere in official AWS documentation, but is relatively easy to verify in practice.