How to clear a Cosmos DB database or delete all items using Azure portal
Solution 1
A Cosmos DB database can contain zero, one, or more Containers. Containers store items. The hierarchy is described here. I am assuming that you want to clear a Container of all items.
Since your connection string is scoped to the database level, the way I quickly clear a Container of all of its items is to just delete and recreate the Container within the database.
To delete a Container in the Azure Portal, do the following:
- In the left menu within the portal, choose All resources -> then choose your Cosmos DB resource to bring up the Cosmos DB management blade.
- Choose Data Explorer. You'll see your databases and each Container listed beneath its database.
- Choose the container you want to delete. Once you highlight the menu item for the Container, click the ... to the right of the Container name. This will have a popup menu where you can choose to delete the container.
For example, if the Container name is users:
Solution 2
One option is to set a TTL of 0 on that particular Container, depending on the number of records though it could take a bit of time.
Alternatively, and this is probably a more viable option, is to simply just to delete & recreate the Container.
chelder
Updated on March 17, 2020Comments
-
chelder over 4 years
If go to https://portal.azure.com, open our Azure Cosmos DB account (1) --> Data Explorer (2) --> Click on users (3) --> Click on New SQL Query:
Azure will open a text box to enter a Query:
I've found that Cosmos DB does not allow to use DELETE instead SELECT: https://stackoverflow.com/a/48339202/1198404, so I should do something like:
SELECT * FROM c DELETE c SELECT * FROM c DELETE *
But any of my attempts worked.
-
chelder almost 5 yearsThanks for the clarification! So how to do it within Azure? :)
-
Rob Reagan almost 5 years@chelder I've added info on how to do this.
-
chelder almost 5 yearsSo clicking on delete is safe? I mean, next time the App Service tries to register a new user, it will be stored on the database normally like nothing was deleted? Thanks! d.pr/i/V8B5xA
-
chelder almost 5 yearsIn my case, after deleting the container I had to publish to Azure our App Service again to create again the container.
-
Rob Reagan almost 5 years@chelder You should be able to recreate the Container right there within the Azure Portal after you delete it.
-
chelder almost 5 yearsOK! Thanks! :-)
-
David Makogon almost 5 yearsStored procedures are scoped to a partition. You can't just clear out a collection this way.
-
widged about 4 yearsThat does not work, unfortunately. With a TTL set to 0, it will display 0 items on the next refresh. However, if the TTL is set to Off afterwards, all items are visible on next refresh.
-
James about 4 years@widged how long are you waiting after this to verify the data hasn't been deleted? Also, how many records? Unless something has changed, this did work, I've not used Cosmos in a little bit.
-
widged about 4 yearsI ended up deleting them manually. They got removed from view. I came back 30 min. later. All my items were back. So, that might be due to a temporary bug. With Azure portal, it is quite common, unfortunately, for functionality to suddenly change, or bugs suddenly appear.
-
James about 4 years@widged yeah doesn't sound right tbh could try setting to 1s incase it's odd behaviour with 0.
-
widged about 4 yearstried that (obviously) but having more new/abnormal behaviour with Cosmos this week. They seem to be making a lot of changes ATM. Best is to wait a month and check again.
-
Rob Reagan about 3 years@chelder And thank you for the illustrations and edits.
-
Red about 2 yearsDo not delete containers! I've just had to create a support request to ask Microsoft to kindly increase the resources in my region because I cannot recreate the container. I spent 30 minutes trying at the end of day yesterday, and 15 minutes at the start of today