Remove Item in DynamoDB Using Java (Guide w/ Code Examples)

Rafal Wilinski

Provided by Rafal Wilinski

To delete an item from a DynamoDB table using the AWS SDK for Java, you can use the deleteItem method of the AmazonDynamoDBClient class. This method enables you to delete an item from a table using its primary key values.

import java.util.Map; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient; import com.amazonaws.services.dynamodbv2.model.AttributeValue; import com.amazonaws.services.dynamodbv2.model.DeleteItemRequest; // Replace "tableName" with the name of your DynamoDB table String tableName = "tableName"; // Create a DynamoDB client AmazonDynamoDBClient client = new AmazonDynamoDBClient(); // Define the primary key values for the item you want to delete Map<String, AttributeValue> key = Map.of( "id", new AttributeValue().withN("123"), // primary key attribute"type", new AttributeValue().withS("foo") // sort key attribute ); // Create a request to delete the item DeleteItemRequest request = new DeleteItemRequest() .withTableName(tableName) .withKey(key); // Delete the item from the table client.deleteItem(request);

Spend less time in the AWS console, use Dynobase.

First 7 days are on us. No strings attached.

Product Features

Member Portal
© 2024 Dynobase
Login to the AWS Console less.
Try Dynobase to accelerate your DynamoDB workflow. Start your 7-day free trial today.