Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete Documents from CosmosDB based on condition through Query Explorer

What's the query or some other quick way to delete all the documents matching the where condition in a collection?
I want something like DELETE * FROM c WHERE c.DocumentType = 'EULA' but, apparently, it doesn't work.

Note: I'm not looking for any C# implementation for this.

like image 317
GorvGoyl Avatar asked Jun 27 '17 07:06

GorvGoyl


People also ask

How do I delete a record on Cosmos DB?

Select the New Stored Procedure button at the top of the Data Explorer section. In the stored procedure tab, locate the Stored Procedure Id field and enter the value: bulkDelete. This stored procedure iterates through all documents that match a specific query and deletes the documents.

How do I delete multiple files in cosmos?

No, you can't delete multiple documents in one go. Each document must be deleted separately. One possible solution would be to write a stored procedure and pass the list of document ids that you want to delete. In that stored procedure, you can loop through that list and delete individual documents.

Which of the below methods will you use to remove a document from the existing collection of a Cosmos DB created using a SQL API?

The Delete Document operation deletes an existing document in a collection.


1 Answers

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

The stored procedure script is 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 make a new stored procedure in the database->collection you need to delete from.

Then right at the bottom of the stored procedure pane, underneath the script textarea is a place to put in the parameter. In my case I just want to delete all so I used:

SELECT c._self FROM c

I guess yours would be:

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

Then hit 'Save and Execute'. Viola, some documents get deleted. After I got it working in the Azure Portal I switched over the Azure DocumentDB Studio and got a better view of what was happening. I.e. I could see I was throttled to deleting 18 a time (returned in the results). For some reason I couldn't see this in the Azure Portal.

Anyway, pretty handy even if limited to a certain amount of deletes per execution. Executing the sp is also throttled so you can't just mash the keyboard. I think I would just delete and recreate the Collection unless I had a manageable number of documents to delete (thinking <500).

Props to Mimi Gentz @Microsoft for sharing the script in the link above.

HTH

like image 198
Ben E G Avatar answered Sep 28 '22 15:09

Ben E G