This document guides you through the process of deleting items from your Azure Cosmos DB collections using the official Java SDK. Deleting items is a fundamental operation for managing data in your NoSQL database.
Deleting an item in Cosmos DB typically involves specifying the item's unique identifier (ID) and, if applicable, its partition key. The SDK provides methods to perform these operations efficiently.
The most common way to delete an item is by providing its unique ID. If your container does not use a partition key, this is all you need.
import com.azure.cosmos.*;
import com.azure.cosmos.models.*;
public class DeleteItemExample {
private static final String ENDPOINT = "YOUR_COSMOS_DB_ENDPOINT";
private static final String PRIMARY_KEY = "YOUR_COSMOS_DB_PRIMARY_KEY";
private static final String DATABASE_NAME = "YOUR_DATABASE_NAME";
private static final String CONTAINER_NAME = "YOUR_CONTAINER_NAME";
public static void main(String[] args) {
// Initialize the Cosmos client
CosmosClient client = new CosmosClientBuilder()
.endpoint(ENDPOINT)
.key(PRIMARY_KEY)
.buildClient();
CosmosDatabase database = client.getDatabase(DATABASE_NAME);
CosmosContainer container = database.getContainer(CONTAINER_NAME);
// The ID of the item to delete
String itemIdToDelete = "your-item-id";
try {
System.out.println("Attempting to delete item with ID: " + itemIdToDelete);
// Delete the item
CosmosResponse itemResponse = container.deleteItem(itemIdToDelete, new PartitionKey(itemIdToDelete)); // If no partition key, you might not need PartitionKey. For partitioned containers, use the actual partition key value.
System.out.println("Item deleted successfully. Status code: " + itemResponse.getStatusCode());
} catch (CosmosException dfe) {
System.err.println("Error deleting item: " + dfe.getStatusCode() + " - " + dfe.getMessage());
} finally {
// Close the client
client.close();
}
}
}
For partitioned containers, you must provide both the item's ID and its partition key value to ensure the correct item is deleted from the relevant partition.
import com.azure.cosmos.*;
import com.azure.cosmos.models.*;
public class DeleteItemPartitionedExample {
private static final String ENDPOINT = "YOUR_COSMOS_DB_ENDPOINT";
private static final String PRIMARY_KEY = "YOUR_COSMOS_DB_PRIMARY_KEY";
private static final String DATABASE_NAME = "YOUR_DATABASE_NAME";
private static final String CONTAINER_NAME = "YOUR_CONTAINER_NAME";
private static final String PARTITION_KEY_PATH = "/yourPartitionKeyPath"; // e.g., "/category"
public static void main(String[] args) {
// Initialize the Cosmos client
CosmosClient client = new CosmosClientBuilder()
.endpoint(ENDPOINT)
.key(PRIMARY_KEY)
.buildClient();
CosmosDatabase database = client.getDatabase(DATABASE_NAME);
CosmosContainer container = database.getContainer(CONTAINER_NAME);
// The ID and partition key of the item to delete
String itemIdToDelete = "your-item-id";
String partitionKeyValue = "your-partition-key-value"; // e.g., "electronics"
try {
System.out.println("Attempting to delete item with ID: " + itemIdToDelete + " and partition key: " + partitionKeyValue);
// Create a PartitionKey object
PartitionKey partitionKey = new PartitionKey(partitionKeyValue);
// Delete the item
CosmosResponse itemResponse = container.deleteItem(itemIdToDelete, partitionKey);
System.out.println("Item deleted successfully. Status code: " + itemResponse.getStatusCode());
} catch (CosmosException dfe) {
System.err.println("Error deleting item: " + dfe.getStatusCode() + " - " + dfe.getMessage());
} finally {
// Close the client
client.close();
}
}
}
It's crucial to implement proper error handling. Common errors include:
404 Not Found
: The item with the specified ID and partition key does not exist.400 Bad Request
: Invalid input, such as an incorrectly formatted partition key.403 Forbidden
: Insufficient permissions.429 Too Many Requests
: Throttling due to exceeding request limits.The SDK throws CosmosException
for errors, allowing you to catch and handle them gracefully.
CosmosClient
: The primary client for interacting with Cosmos DB.CosmosDatabase
: Represents a database in Cosmos DB.CosmosContainer
: Represents a container within a database.container.deleteItem(String id, PartitionKey partitionKey)
: Deletes an item by its ID and partition key.container.deleteItem(String id, Object partitionKeyValue)
: A convenience method for deleting items when the partition key is a simple type.CosmosResponse
: Represents the response from a Cosmos DB operation.CosmosException
: The exception thrown by the SDK for errors.CosmosClient
once and reuse it throughout your application's lifecycle.CosmosException
to manage potential errors and provide informative feedback to users or logs.