DynamoDB DeleteItem in Java (AWS SDK v2)
DeleteItem removes a single item by its full primary key. In AWS SDK for Java 2.x you build a DeleteItemRequest and call deleteItem on a DynamoDbClient; with ReturnValue.ALL_OLD the response tells you whether anything was actually deleted.
Code
import java.util.HashMap;
import java.util.Map;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest;
import software.amazon.awssdk.services.dynamodb.model.DeleteItemResponse;
import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
import software.amazon.awssdk.services.dynamodb.model.ReturnValue;
public class DeleteItemExample {
public static void main(String[] args) {
try (DynamoDbClient ddb = DynamoDbClient.builder()
.region(Region.US_EAST_1)
.build()) {
Map<String, AttributeValue> key = new HashMap<>();
key.put("Artist", AttributeValue.builder().s("Arturo Sandoval").build());
key.put("SongTitle", AttributeValue.builder().s("Cubano Chant").build());
DeleteItemRequest request = DeleteItemRequest.builder()
.tableName("Music")
.key(key)
.returnValues(ReturnValue.ALL_OLD)
.build();
DeleteItemResponse response = ddb.deleteItem(request);
if (response.attributes().isEmpty()) {
System.out.println("No item with that key existed");
} else {
System.out.println("Deleted: " + response.attributes());
}
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
}
}
}Explanation
key— must include every primary-key attribute: the partition key alone for a simple key, or partition and sort key for a composite key.- Delete is idempotent — deleting a key that doesn't exist is still a success (no error). With
returnValues(ReturnValue.ALL_OLD)the deleted item comes back inresponse.attributes(); an empty map means nothing was there. ReturnValuesforDeleteItemsupports onlyNONE(default) andALL_OLD— theALL_NEW/UPDATED_*options belong toUpdateItem.- Conditional delete — add a
conditionExpression(e.g.attribute_exists(Artist)to fail on a missing item, orAwards = :zeroto guard on a value); a failed condition throwsConditionalCheckFailedException. DeleteItemremoves one item per call. There is no "delete all" API — to empty a table, iterate keys withScanand batch the deletes, or drop and recreate the table.
Do it visually
Guarding the delete with a condition? The DynamoDB Expression Builder assembles the ConditionExpression and the name/value maps for you.
To delete items from a GUI — select rows, confirm, done, with the generated code one click away — download DynoTable.
Related examples
- DynamoDB DeleteItem in Go — the same delete with AWS SDK for Go v2.
- DynamoDB PutItem in Java — the write side of the same key.
- DynamoDB condition expressions — guard deletes with
attribute_existsand value checks. - DynamoDB ConditionalCheckFailedException — what a failed conditional delete throws, and when it's expected.
References
- DeleteItem — Amazon DynamoDB API Reference
- Use DeleteItem with an AWS SDK or CLI — Amazon DynamoDB Developer Guide
- DynamoDbClient — AWS SDK for Java 2.x API Reference
- DeleteItemRequest — AWS SDK for Java 2.x API Reference
- Condition expressions — Amazon DynamoDB Developer Guide
Last verified 2026-07-13 against the official AWS documentation linked above.