Delete Multiple Items in DynamoDB Using Java (Guide w/ Code Examples)
Provided by Rafal Wilinski
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 Write Item in DynamoDB Using Java
- Update Expression in DynamoDB Using Java
- Delete Expression in DynamoDB Using Java
- Get in DynamoDB Using Java
- Batch Save Using DynamoDB Mapper
- Batch Read in DynamoDB Using Java
- Query Hash Key Only in DynamoDB Using Java
- Query Index in DynamoDB Using Java
- Batch Load Using DynamoDB Mapper
- Get All Items from DynamoDB Using Java
- Remove Attribute in DynamoDB Using Java
- Delete All Using DynamoDB Mapper
- Query Local Secondary Index in DynamoDB Using Java
- Delete by Hashkey Using DynamoDB Mapper
- Insert in DynamoDB Using Java
Dynobase is a Professional GUI Client for DynamoDB
First 7 days are on us. No strings attached.
Product Features
DynamoDB Tools
DynamoDB Info
© 2024 Dynobase