title: Deleting documents from a Cosmos collection from Azure portal
body: <p>Running the following stored procedure results in zero records getting deleted.</p>
  <pre><code>/**
   * A Cosmos DB stored procedure that bulk deletes documents for a given query.
   * Note: You may need to execute this stored procedure multiple times (depending whether the stored procedure is able to delete every document within the execution timeout limit).
   *
   * @function
   * @param {string} query - A query that provides the documents to be deleted (e.g. &quot;SELECT c._self FROM c WHERE c.founded_year = 2008&quot;). Note: For best performance, reduce the # of properties returned per document in the query to only what's required (e.g. prefer SELECT c._self over SELECT * )
   * @returns {Object.&lt;number, boolean&gt;} Returns an object with the two properties:
   *   deleted - contains a count of documents deleted
   *   continuation - a boolean whether you should execute the stored procedure again (true if there are more documents to delete; false otherwise).
   */
  function bulkDeleteStoredProcedure(query) {
      var collection = getContext().getCollection();
      var collectionLink = collection.getSelfLink();
      var response = getContext().getResponse();
      var responseBody = {
          deleted: 0,
          continuation: true
      };
      
      console.log(query);
  
      // Validate input.
      if (!query) throw new Error(&quot;The query is undefined or null.&quot;);
  
      tryQueryAndDelete();
  
      // Recursively runs the query w/ support for continuation tokens.
      // Calls tryDelete(documents) as soon as the query returns documents.
      function tryQueryAndDelete(continuation) {
          var requestOptions = {continuation: continuation};
  
          var isAccepted = collection.queryDocuments(collectionLink, query, requestOptions, function (err, retrievedDocs, responseOptions) {
              if (err) throw err;
              
              console.log(retrievedDocs.length);
  
              if (retrievedDocs.length &gt; 0) {
                  // Begin deleting documents as soon as documents are returned form the query results.
                  // tryDelete() resumes querying after deleting; no need to page through continuation tokens.
                  //  - this is to prioritize writes over reads given timeout constraints.
                  tryDelete(retrievedDocs);
              } else if (responseOptions.continuation) {
                  // Else if the query came back empty, but with a continuation token; repeat the query w/ the token.
                  tryQueryAndDelete(responseOptions.continuation);
              } else {
                  // Else if there are no more documents and no continuation token - we are finished deleting documents.
                  responseBody.continuation = false;
                  response.setBody(responseBody);
              }
          });
  
          // If we hit execution bounds - return continuation: true.
          if (!isAccepted) {
              response.setBody(responseBody);
          }
      }
  
      // Recursively deletes documents passed in as an array argument.
      // Attempts to query for more on empty array.
      function tryDelete(documents) {
          if (documents.length &gt; 0) {
              // Delete the first document in the array.
              var isAccepted = collection.deleteDocument(documents[0]._self, {}, function (err, responseOptions) {
                  if (err) throw err;
  
                  responseBody.deleted++;
                  documents.shift();
                  // Delete the next document in the array.
                  tryDelete(documents);
              });
  
              // If we hit execution bounds - return continuation: true.
              if (!isAccepted) {
                  response.setBody(responseBody);
              }
          } else {
              // If the document array is empty, query for more documents.
              tryQueryAndDelete();
          }
      }
  }
  </code></pre>
  <p><code>retrievedDocs.length</code> is always 0. This is how I'm calling the procedure:
  <a href="https://i.stack.imgur.com/PfWxn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PfWxn.png" alt="enter image description here" /></a></p>
  <p>query is <code>SELECT c._self FROM c WHERE c.type = 'ORDER'</code></p>
  <p>I have confirmed the above query returns results by querying manually from the portal.</p>
  <p>This code is from <a href="https://stackoverflow.com/a/57723160/2233706">this answer</a>.</p>