Search code examples
c#azure-cosmosdbdelete-rowazure-cosmosdb-sqlapi

deleting document from cosmos db using C# and cosmos client


I am new to Cosmos DB and after reading through the microsoft docs am attempting to delete records from a cosmos db. I have tried to follow the example as closely as possible. The record is being found and read but I am running into a not found error when attempting to delete.

The collection is partitioned, and I have mentioned partition key as mentioned in the examples, but still results in an error.

    private async Task QueryItemsAsync()
    {
        var sqlQueryText = "SELECT * FROM c WHERE c.tenantId = '5c6cb2d77c1c2edc001b9007' and c.id = 'abae8abf-5836-464f-8129-1f0e83a1f639'";

        QueryDefinition queryDefinition = new QueryDefinition(sqlQueryText);
        FeedIterator<Order> queryResultSetIterator = this.container.GetItemQueryIterator<Order>(queryDefinition);

        List<Order> orders = new List<Order>();

        while (queryResultSetIterator.HasMoreResults)
        {
            FeedResponse<Order> currentResultSet = await queryResultSetIterator.ReadNextAsync();
            foreach (Order o in currentResultSet)
            {
                orders.Add(o);
                Console.WriteLine("\tRead {0}\n", o);       
            }
        }
    }         
    private async Task DeleteFamilyItemAsync()
    {
        var partitionKeyValue = "tenantId";
        var orderId = "abae8abf-5836-464f-8129-1f0e83a1f639";

        // Delete an item. Note we must provide the partition key value and id of the item to delete
        ItemResponse<Order> orderResponse = await this.container.DeleteItemAsync<Order>(orderId, new PartitionKey(partitionKeyValue));
        Console.WriteLine("Deleted Family [{0},{1}]\n", partitionKeyValue, orderId);
    }

As seen below the record is being read so it is being found but when I attempt to delete it, it is not being deleted. Read {"Id":null,"patientId":null,"orchestrationId":null,"preOrderId":"88557ec1-dccb-4397-9ce4-20b6aeb1d636","cartId":"adc463ef-7f38-4fa8-8f37-e505db3a5134","confirmationNum":"QDQ-9244014-14708","cartConfirmationNum":"IBM-8622967-14079","countryCode":null,"type":"order","cancelDate":null,"id":"abae8abf-5836-464f-8129-1f0e83a1f639"}

NotFound error occurred: CosmosRequestException;StatusCode=NotFound;SubStatusCode=0;ActivityId=6dd52d23-68c8-4e6d-b4ff-70cd2a6921cf;RequestCharge=1.24;Message=Response status code does not indicate success: 404 Substatus: 0 Reason: ().; End of demo, press any key to exit.


Solution

  • The problem is with your partitionKeyValue. You are providing the partition key definition instead of the value. The value is the actual tenant id not the text tenantId.

    Your delete code should then look like this:

    private async Task DeleteFamilyItemAsync()
    {
        var partitionKeyValue = "5c6cb2d77c1c2edc001b9007"; // <-- This is the change
        var orderId = "abae8abf-5836-464f-8129-1f0e83a1f639";
    
        // Delete an item. Note we must provide the partition key value and id of the item to delete
        ItemResponse<Order> orderResponse = await this.container.DeleteItemAsync<Order>(orderId, new PartitionKey(partitionKeyValue));
        Console.WriteLine("Deleted Family [{0},{1}]\n", partitionKeyValue, orderId);
    }