title: How do I delete a document from CosmosDB using an Azure Cloud Function?
body: <p>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 <a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-cosmos-db-triggered-function" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-cosmos-db-triggered-function</a>.</p>
  
  <p>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?</p>
  
  <p>Here is the code I wrote:</p>
  
  <pre><code>const { id } = req.query;
  context.bindings.outputDocuments = context.bindings.inputDocuments;
  const index = context.bindings.outputDocuments.findIndex(doc =&gt; doc.id === id);
  const doc = context.bindings.outputDocuments.splice(index, 1);
  </code></pre>
  
  <p>I also tried a simpler version but that didn't make a difference:</p>
  
  <pre><code>const { id } = req.query;
  context.bindings.outputDocuments = context.bindings.inputDocuments.filter(doc =&gt; doc.id !== id);
  </code></pre>
  
  <p>I verified using logging statements that my function was correctly updating the outputDocuments for both of the above implementations.</p>
  
  <p>Here are my bindings:</p>
  
  <pre><code>{
    "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
  }
  </code></pre>