Cannot delete Azure Documentdb document: {“Errors”:[“Resource Not Found”]}

Problem

I’m trying to delete a document from an Azure DocumentDB by passing the message Id and it can’t be found. The resulting error message is:

Microsoft. Azure. Documents. DocumentClientException
HResult= 0x80131500
Message= Message:  {“Errors”:[“Resource Not Found”]}

Solution

Set the PartitionKey in RequestOptions, passing the VALUE of the partition key, not the name of the key itself, and send the request options to the DocumentDbRepository.

C# Examples

Deleting an Item

var requestOptions = new RequestOptions { PartitionKey = new PartitionKey(partitionKeyValue) };
await this.client.DeleteDocumentAsync(UriFactory.CreateDocumentUri(this.cosmosDatabase, this.cosmosCollection, id), requestOptions);

Getting an Item

                var requestOptions = new RequestOptions { PartitionKey = new PartitionKey(partitionKeyValue) };

                Document document = await this.client.ReadDocumentAsync(UriFactory.CreateDocumentUri(this.cosmosDatabase, this.cosmosCollection, id), requestOptions);
                return (T)(dynamic)document;

Reference

Thanks to Martin Costello for his answer to a related question on Stack Overflow.

I hope this helps someone!

Happy coding,

Tom Hundley
Elegant Software Solutions

What are your thoughts?

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.