1
votes

I'm using a partitioned collection in azure cosmos db. I inserted documents into the collection. I'm trying to delete a document using the following code, but it is not getting deleted. Throws the following exception.

internal static bool DeleteDocumentUnderPartition(AzureDocumentDbCollectionKind collectionKind, string partitionKey, string id)
    {
        var doc = DocumentClient.CreateDocumentQuery(GetCollectionLink(collectionKind), new FeedOptions
        {
            PartitionKey = new PartitionKey(partitionKey)
        }).AsEnumerable().FirstOrDefault(x => x.Id.Equals(id));
        DocumentClient.DeleteDocumentAsync(doc.SelfLink);            
        return true;
    }

Value for partitionKey passed: "4c1429ca58f84e86a3f2e3d9ba1b74de"
Value for id passed: "aa20ea966258492792864c3817313b2a"

Exception -->

DocumentClientException: Message: {"Errors":["Resource Not Found"]} ActivityId: 96696cde-3ea3-41fb-bc9c-70dfd8cfac36, Request URI: /apps/9dee6cc4-be09-426e-b153-f213b908337a/services/e61dcc20-86c2-4751-b4b7-53fd686d04ae/partitions/42db755a-bded-4fe9-bd4c-3561fae0aaa9/replicas/131599878429038582s, RequestStats:

ResponseTime: 2018-01-18T10:20:07.5146574Z, StoreReadResult: StorePhysicalAddress: rntbd://100.114.47.168:13700/apps/9dee6cc4-be09-426e-b153-f213b908337a/services/e61dcc20-86c2-4751-b4b7-53fd686d04ae/partitions/42db755a-bded-4fe9-bd4c-3561fae0aaa9/replicas/131599878429038582s, LSN: 177530, GlobalCommittedLsn: 177530, PartitionKeyRangeId: , IsValid: True, StatusCode: 0, IsGone: False, IsNotFound: True, RequestCharge: 1, ItemLSN: -1, ResourceType: Collection, OperationType: Read

ResponseTime: 2018-01-18T10:20:07.5146574Z, StoreReadResult: StorePhysicalAddress: rntbd://100.114.45.40:13700/apps/9dee6cc4-be09-426e-b153-f213b908337a/services/e61dcc20-86c2-4751-b4b7-53fd686d04ae/partitions/42db755a-bded-4fe9-bd4c-3561fae0aaa9/replicas/131606373510679694s, LSN: 177530, GlobalCommittedLsn: 177530, PartitionKeyRangeId: , IsValid: True, StatusCode: 0, IsGone: False, IsNotFound: True, RequestCharge: 1, ItemLSN: -1, ResourceType: Collection, OperationType: Read

However, deleting a document from another collection (non-partitioned collection) works for me. Any help would be greatly appreciated.

1
And you are sure Database.Id and collectionKind.ToString() correspond to a database and collection?juunas
@juunas, yes! I'm able to retrieve the document from the same partitioned collection.Vignesh T
What is the value you're passing for the partitionKey?Gaurav Mantri
@GauravMantri It's a string value(some guid value) mapped to the Partition key configured while creating the collectionVignesh T

1 Answers

3
votes

I tried to reproduce your issue using sample code as below but failed.

using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using System;

namespace ConsoleApp1
{
    class Program
    {
        private static DocumentClient client;

        static void Main(string[] args)
        {

            client = new DocumentClient(new Uri("***"), "***");

            var docUri = UriFactory.CreateDocumentUri("db", "collpart", "1");
            var reqOptions = new RequestOptions { PartitionKey = new PartitionKey("1") };
            client.DeleteDocumentAsync(docUri, reqOptions).Wait();

            //block
            Console.ReadLine();
        }
    }
}

My documents in the partition collection(id is my partition key) just like :

enter image description here

I think partition key may be the key of issue. You need to provide the value of the partition key, not the name of the field which stores the partition key.

Or you could try to create a simple new partition collection test if the document can be deleted correctly.

Hope it helps you.


Update Answer:

I think you misunderstand the meaning of partitionkey property in the FeedOptions.

For example , my container is created like this:

enter image description here

The partition key is "name" for my collection here. You could check your collection's partition key.

And my documents as below :

{
    "id": "1",
    "name": "jay"
}

{
    "id": "2",
    "name": "jay2"
}

My partitionkey is 'name', so here I have two paritions : 'jay' and 'jay1'.

So, here you should set the partitionkey property to 'jay' or 'jay2',not 'name'.

If you are not setting the wrong partition key, would you please share your partition key settings and the details of the document you want to delete? (Please hide sensitive information,thank you)