Hemachandra Ghanta
Hemachandra Ghanta

Reputation: 192

How to delete all items in a container from a cosmos database?

I want to read items from an azure cosmos db and delete all

I had hard time using key words and could not use any looping because I have no idea which list/array to loop

this.container.ReadItemAsync(*);

this.container.DeleteItemAsync().All();

Appreciate any Help!

This is how my container looks This is how my container looks

Upvotes: 4

Views: 6702

Answers (1)

David Makogon
David Makogon

Reputation: 71030

The code you showed isn't valid. That is, you cannot just say "delete all documents" like that. And ReadItemAsync() is a direct document-read, based on the document's ID (and partition key).

You would need to query for all documents, and then delete them in your loop (calling DeleteItemAsync() for each ID you found in your query). You'd have to make this query run cross-partition as well (otherwise you'd end up only deleting a subset of your documents).

You can also drop and re-create your collection (which effectively deletes all content in the collection).

Cosmos DB also has a Bulk Executor library (bulk inserts, bulk updates, bulk deletes). Perhaps this will work for you, and avoids having to delete and re-create your collection (just be aware that, if you have a lot of documents, you may run into throttling issues based on your Request Units).

Upvotes: 1

Related Questions