0
votes

I have to delete some documents from azure cosmos DB through azure portal. I wrote a stored procedure in container which will delete the data which has to be deleted. But at the time of execution of stored procedure it will ask for partition key value. I have to delete documents which are having different partition keys.

Below given is the Stored Procedure Used.

function bulkDeleteProcedure(deletedate) {
    var collection = getContext().getCollection();
    var collectionLink = collection.getSelfLink();
    var response = getContext().getResponse();

    var query = "SELECT * FROM c where c._ts = " + deletedate;

    var responseBody = {
        docs_deleted: 0,
        continuation: true
    };

    // Validate input.
    if (!query) throw new Error("The query is undefined or null.");

    fetchData();

    function fetchData(continuation) {
        var requestOptions = {continuation: continuation};

        var isAccepted = collection.queryDocuments(collectionLink, query, requestOptions, function (err, items, responseOptions) {
            if (err) throw err;
            // response.setBody(items);
            if (items.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(items);
                // response.setBody(items.length + " : inside retrvd docs");
            } else if (responseOptions.continuation) {
                // Else if the query came back empty, but with a continuation token; repeat the query w/ the token.
                fetchData(responseOptions.continuation); 
            } else {
                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.
            fetchData();
        }
    }

}

Suppose my partition key is vehicle type and I am having vehicle type values as 01 to 10. The sql query as per my requirement will return documents with 10 different partition key values.

Current scenario is like i have to run the stored procedure 10 times by providing each partition key value each time. Is it possible to run this stored procedure for different partition key values in a single go?

1

1 Answers

1
votes

Current scenario is like i have to run the stored procedure 10 times by providing each partition key value each time. Is it possible to run this stored procedure for different partition key values in a single go?

Unfortunately no. You can't provide multiple partition keys to a stored procedure. You will need to execute the stored procedure 10 times.

One thing you could do is use any available SDK and write code to execute stored procedure in a loop. You could create an array of partition keys and loop through that and execute stored procedure for each partition key in that array.