How to clear a Cosmos DB database or delete all items using Azure portal

34,392

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:

  1. In the left menu within the portal, choose All resources -> then choose your Cosmos DB resource to bring up the Cosmos DB management blade.
  2. Choose Data Explorer. You'll see your databases and each Container listed beneath its database.
  3. 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:

screenshot with example with container named 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.

Share:
34,392
chelder
Author by

chelder

Updated on March 17, 2020

Comments

  • chelder
    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 screenshot 1

    Azure will open a text box to enter a Query:

    Azure screenshot 2

    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
    chelder almost 5 years
    Thanks for the clarification! So how to do it within Azure? :)
  • Rob Reagan
    Rob Reagan almost 5 years
    @chelder I've added info on how to do this.
  • chelder
    chelder almost 5 years
    So 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
    chelder almost 5 years
    In my case, after deleting the container I had to publish to Azure our App Service again to create again the container.
  • Rob Reagan
    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
    chelder almost 5 years
    OK! Thanks! :-)
  • David Makogon
    David Makogon almost 5 years
    Stored procedures are scoped to a partition. You can't just clear out a collection this way.
  • widged
    widged about 4 years
    That 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
    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
    widged about 4 years
    I 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
    James about 4 years
    @widged yeah doesn't sound right tbh could try setting to 1s incase it's odd behaviour with 0.
  • widged
    widged about 4 years
    tried 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
    Rob Reagan about 3 years
    @chelder And thank you for the illustrations and edits.
  • Red
    Red about 2 years
    Do 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