Delete documents from CosmosDB based on condition via Query Explorer

What's a query, or some other quick way to delete all documents matching a where clause in a collection?
 I need something like this DELETE * FROM c WHERE c.DocumentType = 'EULA'

, but it doesn't seem to work.

Note. I am not looking for a C # implementation for this.

+11


source to share


3 answers


I need something like DELETE * FROM c WHERE c.DocumentType = 'EULA' but apparently it doesn't work.



Deleting documents in this way is not supported. You will need to first select the documents using the SELECT query and then delete them separately. If you want, you can write code to extract and delete in a stored procedure and then execute that stored procedure.

+11


source


This is a bit outdated, but had the same requirement and found a concrete example of what @Gaurav Mantri wrote about.

The stored procedure script is located here:

https://social.msdn.microsoft.com/Forums/azure/en-US/ec9aa862-0516-47af-badd-dad8a4789dd8/delete-multiple-docdb-documents-within-the-azure-portal?forum=AzureDocumentDB

Go to the Azure portal, grab the script from above and create a new stored procedure in the database-> collection that you want to delete.

Then right at the bottom of the stored procedure pane, under the textarea script, is where to enter the parameter. In my case, I just want to delete everything, so I used:

SELECT c._self FROM c



I think yours will

SELECT c._self FROM c WHERE c.DocumentType = 'EULA'

Then click "Save and Execute". Viola, some documents are being deleted. After running it in the Azure portal, I switched to Azure DocumentDB Studio and got a better understanding of what was going on. Those. I could see that I was suffocated by deletion 18 times (returned in the results). For some reason, I was unable to see this in the Azure portal.

In any case, it is quite convenient, even if it is limited to a certain number of deletions per execution. The sp execution is also limited, so you can't just wrinkle the keyboard. I think I would just delete and re-create the Collection if I didn't have a manageable number of documents to delete (thinking <500).

Recommends Mimi Gentz ​​@Microsoft for script sharing at the link above.

NTN

+8


source


I wrote a script to list all documents and delete all documents, it can also be modified to delete selected documents.

var docdb = require("documentdb");
var async = require("async");

var config = {
  host: "https://xxxx.documents.azure.com:443/",
  auth: {
    masterKey: "xxxx"
  }
};

var client = new docdb.DocumentClient(config.host, config.auth);

var messagesLink = docdb.UriFactory.createDocumentCollectionUri("xxxx", "xxxx");

var listAll = function(callback) {
  var spec = {
    query: "SELECT * FROM c",
    parameters: []
  };

  client.queryDocuments(messagesLink, spec).toArray((err, results) => {
    callback(err, results);
  });
};

var deleteAll = function() {
  listAll((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();
          }
        });
      });
    }
  });
};

var task = process.argv[2];
switch (task) {
  case "listAll":
    listAll((err, results) => {
      if (err) {
        console.error(err);
      } else {
        console.log(results);
      }
    });
    break;
  case "deleteAll":
    deleteAll();
    break;

  default:
    console.log("Commands:");
    console.log("listAll deleteAll");
    break;
}

      

+3


source







All Articles