1

I figured out how to create, read, and update documents in my CosmosDB instance using Azure Cloud Functions but still have no idea how to implement delete. The main documentation I used as a reference was https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-cosmos-db-triggered-function.

I tried using both an input and output binding without an id (thus fetching the entire collection) and then filtering the item I want to remove from the inputDocuments and setting the result as the outputDocuments. Unfortunately, that doesn't seem to actually delete the item from the database instance. Any idea what I might be missing? What's the correct way to do this?

Here is the code I wrote:

const { id } = req.query;
context.bindings.outputDocuments = context.bindings.inputDocuments;
const index = context.bindings.outputDocuments.findIndex(doc => doc.id === id);
const doc = context.bindings.outputDocuments.splice(index, 1);

I also tried a simpler version but that didn't make a difference:

const { id } = req.query;
context.bindings.outputDocuments = context.bindings.inputDocuments.filter(doc => doc.id !== id);

I verified using logging statements that my function was correctly updating the outputDocuments for both of the above implementations.

Here are my bindings:

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "delete"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "documentDB",
      "name": "inputDocuments",
      "databaseName": "heroesDatabase",
      "collectionName": "HeroesCollection",
      "connection": "ydogandjiev-documentdb_DOCUMENTDB",
      "direction": "in"
    },
    {
      "type": "documentDB",
      "name": "outputDocuments",
      "databaseName": "heroesDatabase",
      "collectionName": "HeroesCollection",
      "createIfNotExists": false,
      "connection": "ydogandjiev-documentdb_DOCUMENTDB",
      "direction": "out"
    }
  ],
  "disabled": false
}
Yuri Dogandjiev
  • 178
  • 1
  • 8
  • Possible duplicate of [Delete document in CosmosDB through azure function](https://stackoverflow.com/questions/46505301/delete-document-in-cosmosdb-through-azure-function) – Mikhail Shilkov Apr 28 '18 at 20:08

1 Answers1

5

You can use the client and call DeleteDocumentAsync as mentioned in the example below

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, string customerid, TraceWriter log, DocumentClient client)
{
    HttpResponseMessage response = new HttpResponseMessage();
    try {
        await client.DeleteDocumentAsync(UriFactory.CreateDocumentUri("BankDatabase","Customers", customerid));
        response.StatusCode = HttpStatusCode.OK;
        log.Info($"Deleted customer with id: {customerid}");
    }
    catch (DocumentClientException exc)
    {
        if (exc.StatusCode == HttpStatusCode.NotFound)
        {
            response = req.CreateResponse(HttpStatusCode.NotFound, "There is no item with given ID in our database!");
        }
        else
        {
            response = req.CreateResponse(HttpStatusCode.InternalServerError, "Internal server error. Contact administrator.");
        }
    }

    return response;
}

Please find the complete Repo on Azure CosmosDB with Azure functions

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • Thanks, do you have an example of how to use the client in a JavaScript function (which is what I’ve been trying to use)? – Yuri Dogandjiev Apr 29 '18 at 22:02
  • 1
    @YuriDogandjiev check this sample https://github.com/Azure/azure-documentdb-node-q/blob/9adf3a6ffd5198c866f5d052bfbc54cb81c26d3a/documentclientwrapper.js – Sajeetharan Apr 30 '18 at 03:05
  • I'm not quite sure I follow your suggestion. In the C# function the client is literally passed in and ready to go. In the sample you pointed me to the client has to be initialized and connected to the database. Is that what you are suggesting I do inside of every execution of my Azure Function? – Yuri Dogandjiev Apr 30 '18 at 06:35