user1224129
user1224129

Reputation: 2779

DocumentDB: Delete a document by ID

I'm using a new database from Microsoft called DocumentDB. Now I want to delete a document by ID, but I cannot figure out, how to do this. Delete operation in DocumentDB requires self-links and they are different from my own ids.

using (var client = new DocumentClient(EndPoint, AuthKey))
{
    await client.DeleteDocumentAsync("**self-link here**");
}

I can execute an additional query to find the self-link and then pass it, but this will require two operations instead one and that is what I'd like to avoid. Is there a better way to remove an entry by ID without using queries or stored procedures?

Upvotes: 10

Views: 8056

Answers (6)

Jevgenij Kononov
Jevgenij Kononov

Reputation: 1239

I was continuously receiving this error: Microsoft.Azure.Documents.DocumentClientException:

Entity with the specified id does not exist in the system.,

The main trick of deleting is PartionKey id. You suppose inside PartionKey provide id like in the code example. I have tried many ways, but I was always receiving different errors...Only this solution worked for me.

await client.DeleteDocumentAsync(input[0].SelfLink, 
  new RequestOptions
  {
     PartitionKey = new PartitionKey(input[0].Id)
  });
Hope this helps :)

Upvotes: 0

Ryan CrawCour
Ryan CrawCour

Reputation: 2728

* UPDATE * This feature has now been implemented

* ORIGINAL ANSWER *

Not today, no. You can head to http://feedback.azure.com/forums/263030-documentdb and vote for the feature there.

Upvotes: 5

Dr. Piyush Dholariya
Dr. Piyush Dholariya

Reputation: 1326

I have tried this code in nodejs to deletebyId and it works for me.

deleteDocumentById: function(params, callback) {
    var self = this,
        query= params.query,
        collection = params.collection;

    client.queryDocuments(collection._self, query, function(err, docs) {
        if (err) {
            return callback(err);
        }
        client.deleteDocument(docs[0]._self, docs[0], function(err, success) {
            if (err) {
                return callback(err);
            }
            callback(null, success);
        });
    });
}

Upvotes: 0

Anu
Anu

Reputation: 233

Here is how I am deleting document

{                
var docUri = UriFactory.CreateDocumentUri(_documentDataBaseId, _documentCollectionId, docId);
                await _documentClient.DeleteDocumentAsync(docUri);
}

Upvotes: 3

Jorge Cupi
Jorge Cupi

Reputation: 161

as there's no solution for this case I'd recommend to retrieve all the documents in the existing collection to get access to the SelfLink and _rid values. I just started a mini wrapper to get access to DocumentDB in Universal Apps and hopefully CrossPlatform using Xamarin: https://github.com/JorgeCupi/documentDB-Win10UAP-wrapper feel free to give me any feedback, participate or request some needed methods.

Upvotes: 0

Related Questions