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 in response.attributes(); an empty map means nothing was there.
  • ReturnValues for DeleteItem supports only NONE (default) and ALL_OLD — the ALL_NEW/UPDATED_* options belong to UpdateItem.
  • Conditional delete — add a conditionExpression (e.g. attribute_exists(Artist) to fail on a missing item, or Awards = :zero to guard on a value); a failed condition throws ConditionalCheckFailedException.
  • DeleteItem removes one item per call. There is no "delete all" API — to empty a table, iterate keys with Scan and 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.

References

Last verified 2026-07-13 against the official AWS documentation linked above.

Work with DynamoDB without the Console

DynoTable is a fast desktop client for DynamoDB — browse tables, run SQL-style queries, and edit items locally.