0
votes

I want to delete two attributes from an item in a DynamoDB table. In the docs and everywhere on the internet it shows removing only one attribute. Is it possible to remove multiple attributes at once from an item in DynamoDB table. If so, how? Below is the code I tried:

const params = {
  TableName: process.env.REPORTS_TABLE,
  Key: {
    ReportId: removeParams.reportId
  },
  UpdateExpression: 'REMOVE #param1, #param2',
  ExpressionAttributeValues: { '#param1': 'StartDate', '#param2': 'EndDate' },
  ReturnValues: 'UPDATED_NEW'
};
const res = await updateReport(params);

I get the below error:

ValidationException: ExpressionAttributeValues contains invalid key: Syntax error; key: "#param2"

Is it a restriction from AWS or is there any other way to do this?

2
The docs clearly indicate that you can use the "REMOVE action in an update expression to remove one or more attributes from an item". Why are you supplying attribute values when you want to remove attributes? - jarmod

2 Answers

5
votes

The documentation shows multiple attributes being removed...

The error message is referring to this line...
ExpressionAttributeValues: { '#param1': 'StartDate', '#param2': 'EndDate' },

ExpressionAttributeValues isn't needed to simply remove attributes...

Try:

const params = {
  TableName: process.env.REPORTS_TABLE,
  Key: {
    ReportId: removeParams.reportId
  },
  UpdateExpression: 'REMOVE #param1, #param2',
  ReturnValues: 'UPDATED_NEW'
};
const res = await updateReport(params);
1
votes

I believe the problem is that you need to pass in ExpressionAttributeNames, not ExpressionAttributeValues.