Delete Multiple Items in DynamoDB Using Java (Guide w/ Code Examples)
To delete multiple items from a DynamoDB table in Java, you can use the batchWriteItem
method of the AmazonDynamoDBClient
class. This method lets you delete up to 25 items in a single request. The example below demonstrates how to use the batchWriteItem
method to delete items from a table called "myTable".
import java.util.ArrayList; import java.util.List; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient; import com.amazonaws.services.dynamodbv2.model.AttributeValue; import com.amazonaws.services.dynamodbv2.model.DeleteRequest; import com.amazonaws.services.dynamodbv2.model.WriteRequest; // Replace "tableName" with the name of your DynamoDB table String tableName = "tableName"; // Create a DynamoDB client AmazonDynamoDBClient client = new AmazonDynamoDBClient(); // Create a list of delete requests List<WriteRequest> requests = new ArrayList<>(); // Add delete requests to the list for (int i = 0; i < 25; i++) { AttributeValue value = new AttributeValue().withN(Integer.toString(i)); DeleteRequest request = new DeleteRequest().withKey(Map.of("id", value)); requests.add(new WriteRequest().withDeleteRequest(request)); } // Delete the items from the table client.batchWriteItem(Map.of(tableName, requests));
Similar Code Examples
- Batch Read in DynamoDB Using Java
- Query Global Secondary Index in DynamoDB Using Java
- Batch Write Item in DynamoDB Using Java
- Query Local Secondary Index in DynamoDB Using Java
- Update Multiple Items in DynamoDB Using Java
- Batch Update Using DynamoDB Mapper
- Query Index in DynamoDB Using Java
- Batch Put Item in DynamoDB Using Java
- Update Expression in DynamoDB Using Java
- Get Multiple Items in DynamoDB Using Java
- Remove Attribute in DynamoDB Using Java
- Batch Insert in DynamoDB Using Java
- Delete in DynamoDB Using Java
- Get All Items from DynamoDB Using Java
- Get in DynamoDB Using Java
Login to the AWS Console less. Use Dynobase.
Try 7-day free trial. No credit card needed.
Product Features
DynamoDB Tools
DynamoDB Info
© 2023 Dynobase