Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear the collection in DocumentDB through Query Explorer

What's the query or some other quick way to remove all the documents in a collection? Right now I'm deleting the whole collection and recreating again.

like image 470
GorvGoyl Avatar asked Apr 24 '17 12:04

GorvGoyl


3 Answers

What's the query or some other quick way to remove all the documents in a collection?

As Chris Pietschmann mentioned that it is not supported currently on the Azure portal, and this feature is underview by Azure Documentdb team.

We could do that with server-side scripts (e.g. stored procedure, udfs, triggers) I get the following code from another SO thread. It works correctly on my side.

/**
 * A DocumentDB stored procedure that bulk deletes documents for a given query.<br/>
 * Note: You may need to execute this sproc multiple times (depending whether the sproc 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 * FROM c WHERE c.founded_year = 2008")
 * @returns {Object.<number, boolean>} Returns an object with the two properties:<br/>
 *   deleted - contains a count of documents deleted<br/>
 *   continuation - a boolean whether you should execute the sproc again (true if there are more documents to delete; false otherwise).
 */
function bulkDeleteSproc(query) {
    var collection = getContext().getCollection();
    var collectionLink = collection.getSelfLink();
    var response = getContext().getResponse();
    var responseBody = {
        deleted: 0,
        continuation: true
    };

    // 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;

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

More detail steps do on the Azure portal are as follwoing:

  1. Check the number of document in a collection

enter image description here

  1. Create a produce store in the collection

enter image description here

  1. Check that all of documents in the collection have been deleted

enter image description here

like image 69
Tom Sun - MSFT Avatar answered Nov 17 '22 21:11

Tom Sun - MSFT


In test environments, we have found the trick of setting the TTL to 1 sec, waiting for cosmosdb to do its thing, and change TTL back to normal.

Happily this can be done in the azure portal.

In the background, Cosmosdb deletes all the documents itself, but it does take time.

example: if you have 1000 documents on a collection and ttl is off

select count(1) from c = 1000

Set TTL = 1 sec

select count(1) from c = 0

but if you switch normal TTL back on and do the count before its had time to delete all the documents in the background, you get the same number as it was before setting TTL to 1 sec. It takes time to delete them all in the background.

like image 44
alfan Avatar answered Nov 17 '22 21:11

alfan


make query that returns selflink + partition key (here= company.id), then delete every document

protected async Task DeleteAllDocumentsAsync()
    {
        DocumentClient client = createClient();
        //**make query that returns selflink + partition key (here= company.id)**
        var docs = client.CreateDocumentQuery("your collection uri", "select c._self, c.company.id from c", new FeedOptions() {EnableCrossPartitionQuery = true}).ToList();

        foreach (var doc in docs)
        {
            var requestOptions = new RequestOptions() {PartitionKey = new PartitionKey(doc.id)};
            await client.DeleteDocumentAsync(doc._self, requestOptions);
        }
    }
like image 1
hannes neukermans Avatar answered Nov 17 '22 23:11

hannes neukermans