Delete by Hashkey Using DynamoDB Mapper (Guide w/ Code Examples)

Provided by Rafal Wilinski
Deleting an item in a table using only the partition key is called a "delete by hash key" operation. In Java, you can use the DynamoDBMapper
class to perform a delete by hash key, by creating an instance of the domain class you want to delete and specifying the primary key value.
In the following code, the DynamoDBMapper
class is used to perform a delete by hash key, by creating an instance of the Product
domain class, specifying the primary key value, and then calling the delete
method. The delete
method accepts an instance of the domain class object, and it works by checking the primary key value, if the primary key value is present in the item, the item will be deleted.
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper; public class DeleteByHashKeyExample { public static void main(String[] args) { // Create a new DynamoDBMapper AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build(); DynamoDBMapper mapper = new DynamoDBMapper(client); // Define the hash key value to delete String hashKeyValue = "123"; // Define the item to delete Product itemToDelete = new Product(); itemToDelete.setId(hashKeyValue); // Perform the delete mapper.delete(itemToDelete); } }
Similar Code Examples
- Update Expression in DynamoDB Using Java
- Query Date Range in DynamoDB Using Java
- Remove with DynamoDB Mapper
- Batch Save Using DynamoDB Mapper
- Query Local Secondary Index in DynamoDB Using Java
- Get Item Request in DynamoDB Using Java
- Batch Load Using DynamoDB Mapper
- Batch Insert in DynamoDB Using Java
- Get All Items from DynamoDB Using Java
- Batch Read in DynamoDB Using Java
- Conditional Update in DynamoDB Using Java
- Batch Write Using DynamoDB Mapper
- Batch Query in DynamoDB Using Java
- Batch Get in DynamoDB Using Java
- Query Global Secondary Index in DynamoDB Using Java
Tired of switching accounts and regions? Use Dynobase.
Try 7-day free trial. No credit card needed.
Product Features
DynamoDB Tools
DynamoDB Info
© 2025 Dynobase