Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cosmos DB - Deleting a document

How can I delete an individual record from Cosmos DB?

I can select using SQL syntax:

SELECT * FROM collection1 WHERE (collection1._ts > 0) 

And sure enough all documents (analogous to rows?) are returned

However this doesn't work when I attempt to delete

DELETE FROM collection1 WHERE (collection1._ts > 0) 

How do I achieve that?

like image 971
Ben Mayo Avatar asked Oct 22 '17 08:10

Ben Mayo


People also ask

How do I delete data from Cosmos database?

Power Automate to delete data from Cosmos DBAdd a new step, “Delete a document”, which will get all the rows from the Cosmos DB. In case you are not able to find it, then you should search for “Cosmos” and under that you will be able to select the “Delete a document” step as shown in the below screen shot.

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.

How do I delete multiple records in cosmos?

You cannot delete multiple documents, but you can use stored procedure to delete documents in one partition.


2 Answers

The DocumentDB API's SQL is specifically for querying. That is, it only provides SELECT, not UPDATE or DELETE.

Those operations are fully supported, but require REST (or SDK) calls. For example, with .net, you'd call DeleteDocumentAsync() or ReplaceDocumentAsync(), and in node.js, this would be a call to deleteDocument() or replaceDocument().

In your particular scenario, you could run your SELECT to identify documents for deletion, then make "delete" calls, one per document (or, for efficiency and transactionality, pass an array of documents to delete, into a stored procedure).

like image 193
David Makogon Avatar answered Oct 03 '22 08:10

David Makogon


The easiest way is probably by using Azure Storage Explorer. After connecting you can drill down to a container of choice, select a document and then delete it. You can find additional tools for Cosmos DB on https://gotcosmos.com/tools.

Use Azure Storage Explorer to connect to Cosmos DB

like image 23
Andreas Ågren Avatar answered Oct 03 '22 09:10

Andreas Ågren