0
0
DynamodbHow-ToBeginner · 4 min read

How to Use DELETE in Update Expression in DynamoDB

In DynamoDB, use the DELETE action in an UpdateExpression to remove specific elements from a set attribute. The syntax is DELETE attributeName :value, where :value is a set of elements to remove. This only works with set types, not lists or maps.
📐

Syntax

The DELETE action in a DynamoDB UpdateExpression removes specified elements from a set attribute. It requires the attribute name and a value placeholder representing the set elements to delete.

  • DELETE: The action keyword to remove elements from a set.
  • attributeName: The name of the set attribute to update.
  • :value: A placeholder for the set of elements to remove, defined in ExpressionAttributeValues.
json
UpdateExpression: "DELETE attributeName :value"

ExpressionAttributeValues: {
  ":value": {"SS": ["element1", "element2"]}
}
💻

Example

This example shows how to remove elements from a set attribute named Tags in a DynamoDB item. It deletes the tags "blue" and "green" from the set.

javascript
const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB();

const params = {
  TableName: 'Products',
  Key: {
    'ProductId': { S: '123' }
  },
  UpdateExpression: 'DELETE Tags :tagsToRemove',
  ExpressionAttributeValues: {
    ':tagsToRemove': { SS: ['blue', 'green'] }
  },
  ReturnValues: 'UPDATED_NEW'
};

dynamodb.updateItem(params, (err, data) => {
  if (err) {
    console.error('Error updating item:', err);
  } else {
    console.log('Update succeeded:', JSON.stringify(data.Attributes));
  }
});
Output
Update succeeded: {"Tags":{"SS":["red","yellow"]}}
⚠️

Common Pitfalls

  • DELETE only works on set attributes: Trying to use DELETE on lists or maps will cause an error.
  • Value must be a set: The value in ExpressionAttributeValues must be a set type (e.g., SS for string sets).
  • Elements not in the set are ignored: Deleting elements not present in the set does not cause errors but has no effect.
  • Attribute must exist and be a set: If the attribute does not exist or is not a set, the update will fail.
json
/* Wrong usage: Trying to DELETE from a list attribute */
UpdateExpression: "DELETE Colors :vals"
ExpressionAttributeValues: {
  ":vals": { "L": [ { "S": "red" } ] }
}

/* Correct usage: DELETE from a set attribute */
UpdateExpression: "DELETE Colors :vals"
ExpressionAttributeValues: {
  ":vals": { "SS": ["red"] }
}
📊

Quick Reference

ConceptDescription
DELETERemoves elements from a set attribute in DynamoDB.
AttributeMust be a set type (String Set, Number Set, or Binary Set).
ExpressionAttributeValuesMust provide a set of elements to remove using the correct set type (SS, NS, BS).
EffectRemoves specified elements if they exist; no error if elements are missing.
UnsupportedCannot delete elements from lists or maps using DELETE.

Key Takeaways

Use DELETE in UpdateExpression to remove elements only from set attributes in DynamoDB.
The value for DELETE must be a set type matching the attribute's set type (SS, NS, or BS).
DELETE ignores elements not present in the set and does not cause errors for missing elements.
Do not use DELETE on lists or maps; it only works with sets.
Ensure the attribute exists and is a set before using DELETE to avoid update errors.