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

chelder picture chelder · Aug 17, 2019 · Viewed 18.2k times · Source

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.

Answer

Rob Reagan picture Rob Reagan · Aug 18, 2019

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