0
votes

I am very new to cosmos Db and have a requirement of deleting a complete partition. On performing a brief research I found that drop partition is not a thing. Consequently i stumbled on the following link which is a stored procedure for bulk delete

https://github.com/Azure/azure-cosmosdb-js-server/blob/master/samples/stored-procedures/bulkDelete.js

I created this stored procedure on my collection and clicked 'Execute'. I got the following prompt

enter image description here

I entered the partition key and input parameter to 'select * from c'. However, I can see that only 38 documents are deleted at a time and the query completes successfully. Are there any setttings that are forcing the stored procedure to stop prematurely?

2
Hi,your link and your image are gone.Please fix them so that we could get more details. - Jay Gong
Thanks. I have updated the image. The problem is described in the last two lines. I am using the stored procedure from the first link. - lohiarahul
Have you checked the number of the retrieved documents by sql and the number of deleted documents.Are they equal? No any error occurred? You could use console.log() to locate my question. - Jay Gong
Hi,any progress? - Jay Gong
It depends on the RUs allocated. You need a continuation token to keep re-trying - lohiarahul

2 Answers

2
votes

As you mentioned in the comment, your situation is depends on the RUs allocated.I provide you with below sample stored procedure code.You could refer to it.

function deleteSproc(query) {
    var collection = getContext().getCollection();
    var collectionLink = collection.getSelfLink();
    var response = getContext().getResponse();
    var responseBody = {
        deleted: 0,
        continuation: ""
    };

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

    tryQueryAndDelete();

    function tryQueryAndDelete(continuation) {
        var requestOptions = {
            continuation: continuation, 
            pageSize: 10
        };

        var isAccepted = collection.queryDocuments(collectionLink, query, requestOptions, function (err, documents, responseOptions) {
            if (err) throw err;

            if (documents.length > 0) {
                tryDelete(documents);
                if(responseOptions.continuation){
                    tryQueryAndDelete(responseOptions.continuation);
                }else{
                    response.setBody(responseBody);
                }

            }
        });

        if (!isAccepted) {
            response.setBody(responseBody);
        }
    }

    function tryDelete(documents) {
        if (documents.length > 0) {
            var requestOptions = {etag: documents[0]._etag};

            // Delete the document.
            var isAccepted = collection.deleteDocument(
                documents[0]._self, 
                requestOptions, 
                function (err, updatedDocument, responseOptions) {
                    if (err) throw err;

                    responseBody.deleted++;
                    documents.shift();
                    // Try updating the next document in the array.
                    tryDelete(documents);
                }
            );

            if (!isAccepted) {
                response.setBody(responseBody);
            }
        } 
    }
}

Furthermore, as I know, stored procedure has 5 seconds execute limitation. If you crash into the timeout error, you could pass the continuation token as parameter into stored procedure and execute stored procedure several times.

Any concern, please let me know. Thank you.

0
votes

If you are creating the above stored procedure then make sure while execute you will give the Value of partition key not the name of partition key column.

Also Cosmos DB stored procedure will run only in one partition at a time and it will be the same which you are passing in Input (partition key value).