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
- Update Expression in DynamoDB Using Java
- Query Date Range in DynamoDB Using Java
- Remove with DynamoDB Mapper
- Delete All Using DynamoDB Mapper
- Update Item in DynamoDB Using Java
- Query Global Secondary Index in DynamoDB Using Java
- Update Attribute in DynamoDB Using Java
- Query Index in DynamoDB Using Java
- Query Hash Key Only in DynamoDB Using Java
- Get Item Request in DynamoDB Using Java
- Delete All Items in DynamoDB Using Java
- Delete in DynamoDB Using Java
- Conditional Update in DynamoDB Using Java
- Insert in DynamoDB Using Java
- Batch Load Using DynamoDB Mapper
Tired of AWS Console? Try Dynobase.
First 7 days are. No credit card needed.
Product Features
DynamoDB Tools
DynamoDB Info
© 2024 Dynobase