Reputation: 1031
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.
Upvotes: 11
Views: 21807
Reputation: 1031
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);
For Kotlin:
dynamoDB.updateItem( UpdateItemRequest(
"users",
Key(AttributeValue().withN(123L.toString()),
mapOf("555" to AttributeValueUpdate(null, "DELETE"))
))
Upvotes: 14
Reputation: 32808
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.
Upvotes: 9
Reputation: 2050
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.
Upvotes: 0