Delete Multiple Items in DynamoDB Using Java (Guide w/ Code Examples)
![Rafal Wilinski](https://pbs.twimg.com/profile_images/1390706809548197895/SVCKpLNE_400x400.png)
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 Put Item in DynamoDB Using Java
- Batch Write Item in DynamoDB Using Java
- Update Attribute in DynamoDB Using Java
- Insert in DynamoDB Using Java
- Delete Record in DynamoDB Using Java
- Query Local Secondary Index in DynamoDB Using Java
- Batch Delete Using DynamoDB Mapper
- Delete All Using DynamoDB Mapper
- Update Multiple Items in DynamoDB Using Java
- Update Item in DynamoDB Using Java
- Remove Item in DynamoDB Using Java
- Update Expression in DynamoDB Using Java
- GetItem in DynamoDB Using Java
- Remove with DynamoDB Mapper
- Batch Get in DynamoDB Using Java
Tired of switching accounts and regions? Use Dynobase.
Try 7-day free trial. No strings attached.
Product Features
DynamoDB Tools
DynamoDB Info
© 2024 Dynobase