Search code examples
c#azure-cosmosdb

How to delete object from Cosmos DB without knowing the partition key value?


If I don't have the partition key value, how do I delete a document?

I have the id of the document and the property name of the partition key (in my case: type).

I tried:

var docLink = UriFactory.CreateDocumentUri(databaseName, collectionName, documentId);
var resp = client.DeleteDocumentAsync(docLink).Result;

Got error: PartitionKey value must be supplied for this operation.

Example document:

{
   "id": "AB12CD", 
   "type": "Company", 
   "Address": "123 Test Street"
}

I tried to get the partition key value for the specific document id

Using C# code...

I tried to read the document by its id and then pull the type value, but I got error: InvalidOperationException: PartitionKey value must be supplied for this operation. for using the following code.

var response = client.ReadDocumentAsync(UriFactory.CreateDocumentUri(databaseName, collectionName, id)).Result;

I also tried a Cross Partition Query by trying:

FeedOptions queryOptions = new FeedOptions { MaxItemCount = 10 };
queryOptions.EnableCrossPartitionQuery = true;
var queryString =  "SELECT * FROM c WHERE c.id= '" + id + "'";
var QueryInSql = client.CreateDocumentQuery<JObject>(
                documentCollectionUri,,
                queryOptions).AsDocumentQuery();
var result = QueryInSql.ExecuteNextAsync<JObject>().Result;

res = result.ToList<JObject>(); //if result has nothing, will be empty list

^ returns an empty list. If I check on the Azure Portal, I can see the document with that specific id does exist in the db.


Solution

  • Just refer to this doc, you will find the answer.

    In C# code, please use Undefined.Value and everything will be fine.

    client.DeleteDocumentAsync(
              UriFactory.CreateDocumentUri(DbName, CollectionName, id), 
              new RequestOptions() { PartitionKey = new PartitionKey(Undefined.Value) });
    

    Hope it helps you.


    Update Answer:

    Maybe I misunderstanding your requirement yesterday, let me make a new clarification here.

    Firstly, you collection is partitioned by partition field: type.

    1.If you do not know the value of partition key and the document does have a partition field ,please use EnableCrossPartitionQuery property.

    FeedOptions queryOptions = new FeedOptions
            {
                MaxItemCount = 10,
                EnableCrossPartitionQuery = true
            };
     var id = '1';
     var queryString = "SELECT * FROM c WHERE c.id= '" + id + "'";
     var QueryInSql = client.CreateDocumentQuery<JObject>(
                            uri,
                            queryString,
                            queryOptions).AsDocumentQuery();
     var result = QueryInSql.ExecuteNextAsync<JObject>().Result;
    
     var res = result.ToList<JObject>(); //if result has nothing, will be empty list
    
    
     Console.WriteLine("\nRead {0}", res[0]);
    

    2.If you know the value of partition key and the document does have a partition field,please pass the partition key property in FeedOptions.

    3.If you know the document does not have partition field in a partitioned collection,please use Undefined.Value.