in vnext/code-samples/dotnet/src/cosmosdb-vnext-test/Program.cs [189:228]
private async Task DeleteDocumentAndVerifyAsync(Container container, string id, string partitionKey)
{
Console.WriteLine($"Deleting document {id} with partition key {partitionKey}");
try
{
// Delete the document
ItemResponse<TestDocument> deleteResponse = await container.DeleteItemAsync<TestDocument>(
id: id,
partitionKey: new PartitionKey(partitionKey)
);
Console.WriteLine($"Deleted document - Status: {deleteResponse.StatusCode}");
// Verify deletion by attempting to read the document (should fail)
Console.WriteLine("Verifying deletion by attempting to read the document:");
try
{
ItemResponse<TestDocument> verifyResponse = await container.ReadItemAsync<TestDocument>(
id: id,
partitionKey: new PartitionKey(partitionKey)
);
// If we get here, deletion failed
Console.WriteLine("Warning: Document still exists after deletion attempt!");
}
catch (CosmosException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
{
Console.WriteLine("Verified: Document was successfully deleted (not found anymore)");
}
}
catch (CosmosException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
{
Console.WriteLine($"Document with id {id} not found!");
}
catch (CosmosException ex)
{
Console.WriteLine($"Error deleting document: {ex.StatusCode} - {ex.Message}");
}
}