How to use a stored procedure in Cosmos to delete entire collection?

Viewed 93

I and working with Cosmos DB, I have a large collection of items and I would like to delete the entire collection and just recreate it. Is this possible with a stored procedure?

I haven't seen any doc on it. My thought was to write a stored procedure and then run that before my work flow so that the data is fresh, and the delete happens fast.

1 Answers

If you're talking about deleting all documents in a collection: A stored procedure will be limited to a single partition within a collection, which will require multiple calls to such a stored procedure. And you would need to query for partition key ranges so that you can make these multiple calls.

There's really no way to tell how long this will take to run, as it will be highly dependent on the size of your collection, RU/sec allocation, indexing, etc.

If you're talking about deleting the collection via stored procedure: a collection-delete operation is not something you should be doing via stored procedure (I'm not sure it's even possible, especially since a stored procedure is associated with a collection). That's a management API call you should make on your own (well, two calls: one to delete the collection, one to create the collection).

Related