How to delete all documents in Cosmosdb Collection

I have been working with couple of applications built with CosmosDB and one of the things that surprised me was one cannot clear all documents in a collection from the Azure web portal or using the Storage Explorer. As I was struggling to do this while doing some tests on the application I decided to write a blog on the solution I used. There are two ways to achieve the same

  • Using a stored procedure
  • Using Cosmosdb SDK

Using Cosmosdb SDK:

I came up with a script in Node which can be done with any of the programming languages such as C#,Python supported by the SDK

Let’s go through the steps:

Step 1:

Open VScode and Create a file named cosmosdb_helper.js

Step 2:

Let’s install the necessary packages needed.

Install documentdb javascript sdk with the following command,

npm i documentdb

and you will see the output as follows

2019-02-04_21-36-10

Let’s install require to handle the dependencies with the following command,

npm i require

and you will see the output as follows,

2019-02-04_21-36-31

Step 3: 

Let’s do some coding. You will be able to understand the following code with the comments added on each line,

//lets import the necessary packages
var docdb = require("documentdb");
var async = require("async");
// Get the host link which is the documentdb/cosmosdb url from the portal and masterkey is the authentication key
var config = {
host: "https://identityshareddocdb.documents.azure.com:443/",
auth: {
masterKey: "2XEDLvGfedNBDZMAv+EXnZ0SFR4a4MuVt1Fi6WmrRwH7SLcHrr0MvCFm9v+W83qfcJBJXedWjxT47PnNyB1DwA=="
}
};
// Create the CosmosDB client
let client = new docdb.DocumentClient(config.host, config.auth);
// Creating the link
let documentsLink = docdb.UriFactory.createDocumentCollectionUri("IdentityEventLog", "Events");
// Retrive all documents
let selectAll = function(callback) {
var spec = {
query: "SELECT * FROM c",
parameters: []
};
client.queryDocuments(documentsLink, spec).toArray((err, results) => {
callback(err, results);
});
};
// Retrieve and delete all documents
let deleteAll = function() {
selectAll((err, results) => {
if (err) {
console.log(err);
} else {
async.forEach(results, (message, next) => {
client.deleteDocument(message._self, err => {
if (err) {
console.log(err);
next(err);
} else {
next();
}
});
});
}
});
};
// Getting the input whether selectAll or deletAll
let task = process.argv[2];
switch (task) {
case "selectAll":
selectAll((err, results) => {
if (err) {
console.error(err);
} else {
console.log(results);
}
});
break;
case "deleteAll":
deleteAll();
break;
default:
console.log("Commands:");
console.log("selectAll deleteAll");
break;
}
view raw cosmosdb helper hosted with ❤ by GitHub

Suppose if you have partitionKey created with your collection, you need to pass queryoptions with the partitionKey in selectAll as well as deletDocument as follows,

//selectAll
client.queryDocuments(documentsLink, spec,{ enableCrossPartitionQuery: true }).toArray((err, results) =>
//deletDocument
var options = {
partitionKey:'SyncId'
};
client.deleteDocument(message._self,options, err => {

Step 4:

Let’s run the script and see the output,

You can run the helper script as follows,

if you want to list all documents in the collection,

node cosmosdb_helper.js selectAll

which will list the output of all documents in the collection.

If you want to delete all documents within a collection, you can run the script as,

node cosmosdb_helper.js deletAll

which will remove all documents in the collection.

Using a stored procedure:

As mentioned above, 2nd way is to use the stored procecure given by Microsoft employee as mentioned here.

Hope the helper script will help someout out there in order to delete all documents in a collection. You can get the whole code from Cosmosd_Helper

%d bloggers like this: