1
votes

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://docs.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
}
1

1 Answers

5
votes

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