9
votes

I'm trying to figure out the best way to delete an attribute from an item in Dynamo DB. Below is what I tried, but I get an exception saying that DELETE is not a supported for either type N or S.

Exception in thread "main" Status Code: 400, AWS Service: AmazonDynamoDB, AWS Request ID: 09MRO4PVTJ8IK6OHLKSM551REJVV4KQNSO5AEMVJF66Q9ASUAAJG, AWS Error Code: ValidationException, AWS Error Message: One or more parameter values were invalid: Action DELETE is not supported for the type N at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:544) at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:284) at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:169) at >com.amazonaws.services.dynamodb.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:675) at >com.amazonaws.services.dynamodb.AmazonDynamoDBClient.updateItem(AmazonDynamoDBClient.java:371)

Key pk = new Key(new AttributeValue().withN(Long.toString(123)));
AttributeValueUpdate avu = new AttributeValueUpdate(new AttributeValue().withN("555"), "DELETE");
Map<String, AttributeValueUpdate> m = new HashMap<String, AttributeValueUpdate>();
m.put(String.valueOf(555), avu);
UpdateItemRequest uir = new UpdateItemRequest("users", pk, m);
dynamoDB.updateItem(uir);

One point of confusion is why the attribute value matters for a deletion. I really want to delete an attribute name and any associated values, but couldn't find the appropriate way to do that in the SDK.

Help would be appreciated.

3
Could you post the actual exceptionUsman Ismail
@Layble: Glad you managed to resolve this yourself - could you please post your solution as an answer and accept it? Following up to your own question is perfectly reasonable and desired, as long as it has been a real open question in the first place. Thanks :)Steffen Opel

3 Answers

12
votes

I could have sworn I already tried this but by replacing the AttributeValue with a null value it works:

Key pk = new Key(new AttributeValue().withN(Long.toString(123)));
AttributeValueUpdate avu = new AttributeValueUpdate(null, "DELETE");
Map<String, AttributeValueUpdate> m = new HashMap<String, AttributeValueUpdate>();
m.put(String.valueOf(555), avu);
UpdateItemRequest uir = new UpdateItemRequest("users", pk, m);
dynamoDB.updateItem(uir);
8
votes

This also works.

Table table = dynamoDB.getTable("users");
table.updateItem(new PrimaryKey("<MY HASH KEY NAME>", <MY HASH VALUE>), new AttributeUpdate("columnToRemove").delete());

or you can even use Expressions in an Item Update.

REMOVE - Removes one or more attributes from an item.

0
votes

To delete an Attribute from Dynamo DB while Updating an item of the table

 UpdateItemRequest updateItemRequest =new UpdateItemRequest().withTableName(tableName).withKey(key);
 updateItemRequest.addAttributeUpdatesEntry("privileges",new AttributeValueUpdate()
          .withAction(AttributeAction.DELETE));

updateItemRequest.addAttributeUpdatesEntry("description", new AttributeValueUpdate()
          .withValue(new AttributeValue().withS("description")));

In above Example, First I removed privileges from Item and then updated description in Item.