coderman
coderman

Reputation: 1514

Azure Cosmos DB - Delete entire partition

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?

Upvotes: 1

Views: 6530

Answers (3)

Dilpreet
Dilpreet

Reputation: 37

We can now delete items by partition key. The feature is currently in public preview with Java and .NET SDK support also available in preview packages.

Upvotes: 0

Jay Gong
Jay Gong

Reputation: 23782

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.

Upvotes: 2

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).

Upvotes: 0

Related Questions