Search code examples
javanosqlamazon-dynamodb

Deleting Attribute in DynamoDB


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.


Solution

  • 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"))
    ))