Ramon de Klein
Ramon de Klein

Reputation: 5706

DeleteItemAsync is not returned the item being deleted

Container.DeleteItemAsync returns an ItemResponse<T> that holds a Resource property. This implies that the call will return the document that is actually deleted.

But the Resource property is always null and the status is set to NoContent, so it seems that this call never returns the document that is actually deleted. Why would it return a typed response, when it never returns an instance of the type. Also this MS blog describes how the response can explicitly be disabled when deleting documents. This also implies that the item should be deleted.

I have checked the database, but the document is actually being deleted. Trying to delete a non-existing document results in a NotFound status and raises an exception.

I have also tried setting EnableContentResponseOnWrite = true, but this doesn't do anything. I tried Microsoft.Azure.Cosmos v3.14 and v3.19. Both had the same result...

Upvotes: 2

Views: 1876

Answers (1)

Noah Stahl
Noah Stahl

Reputation: 7613

The DeleteItemAsync return type is a bit misleading given that ItemResponse<T> suggests a populated T with the deleted item but is actually null.

Perhaps a clearer approach is to use DeleteItemStreamAsync and inspect the response code, like this:

using ResponseMessage response = await container.DeleteItemStreamAsync(itemId, new(partitionKey), options);
if (response.StatusCode == HttpStatusCode.NoContent)
{
    // Success
}
else
{
    // Failed
}

Now you avoid exception-throwing overhead, and the code doesn't mislead about content being non-null.

Upvotes: 3

Related Questions