Deleting Documents - ( CosmosDB with .NET )
Syntax
public Task DeleteItemAsync(
PartitionKey partitionKey,
string id,
RequestOptions requestOptions = null,
CancellationToken cancellationToken = null
)
Example
await container.DeleteItemAsync<ToDoItem>("partition-key-value", "item-id");
Output
This method does not return any output.
Explanation
In Cosmos DB, a document can be deleted using the DeleteItemAsync
method provided by the Container
class. This method takes in the partition key and the id of the document to be deleted as parameters.
In the example provided above, we are deleting a document of type ToDoItem
with a partition key value of "partition-key-value"
and an id of "item-id"
.
Use
The DeleteItemAsync
method can be used to delete individual documents in a Cosmos DB container. This method can be used in scenarios where a particular document needs to be deleted due to changes in business requirements or data cleanup.
Important Points
- The
DeleteItemAsync
method deletes a single document from a container based on the partition key value and id. - Deleting a document is a permanent operation and cannot be undone.
- The
Container
class provides several other methods to delete documents, such asDeleteItemStreamAsync
andDeleteItemsAsync
.
Summary
In this tutorial, we learned how to delete documents from a Cosmos DB container using the DeleteItemAsync
method provided by the Container
class. We also discussed the syntax, example, output, explanation, use, important points, and summary of deleting documents in Cosmos DB.