1

Running the following stored procedure results in zero records getting deleted.

/**
 * 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. "SELECT c._self FROM c WHERE c.founded_year = 2008"). 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.<number, boolean>} 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("The query is undefined or null.");

    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 > 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 > 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();
        }
    }
}

retrievedDocs.length is always 0. This is how I'm calling the procedure: enter image description here

query is SELECT c._self FROM c WHERE c.type = 'ORDER'

I have confirmed the above query returns results by querying manually from the portal.

This code is from this answer.

user2233706
  • 6,148
  • 5
  • 44
  • 86
  • You need to specify the actual value of the PartitionKey. I believe you're specifying the name of the PartitionKey attribute (`id`). – Gaurav Mantri Sep 02 '21 at 14:31
  • Could you clarify what you mean by 'actual value'? – user2233706 Sep 02 '21 at 14:35
  • instead of "/id" pass the actual value of the id – Sajeetharan Sep 02 '21 at 14:36
  • The value of the `id` attribute in the documents you wish to delete. – Gaurav Mantri Sep 02 '21 at 14:36
  • I don't understand. When I pass ```id```, I get no records. I can't pass a specific value because the id is a GUID, and each document has a different value. – user2233706 Sep 02 '21 at 14:44
  • And `id` is also the PartitionKey for the container? – Gaurav Mantri Sep 02 '21 at 14:44
  • Yes, ```id``` is the partition key. – user2233706 Sep 02 '21 at 14:46
  • 2
    Then I don't think it would make any sense to use a stored procedure to delete the documents. Execution of a stored procedure is scoped to a logical partition and you would need to specify the value of the partition key for its execution. Considering each document in your container belongs to a separate logical partition, you may be better off querying the documents and deleting the documents from the client side. – Gaurav Mantri Sep 02 '21 at 14:49
  • Is there anyway to do this from the portal? Would a user defined function work? – user2233706 Sep 02 '21 at 14:51
  • 1
    UDF will also not work. I don't think Azure Portal allows you to delete multiple documents. You will need to delete one document at a time. You could always write some code using any available SDK. If you're looking for a tool to do so, may I suggest you look at [`Cerebrata`](https://www.cerebrata.com). It's not free though. Disclosure: I am part of the team behind this product. – Gaurav Mantri Sep 02 '21 at 14:55

1 Answers1

1

This stored procedure will never work deleting records in bulk.

Your container is partitioned by /id. This effectively turns Cosmos into a key-value store with one document per partition. Since stored procedures are scoped to a single partition, this stored procedure would only be able to delete one record at a time effectively making it useless to do any bulk operations.

The only way you can do bulk operations on this data is to copy it to a new container with a different partition key.

Mark Brown
  • 8,113
  • 2
  • 17
  • 21