Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 864
chelder Avatar asked Aug 17 '19 20:08

chelder


People also ask

How do I delete multiple items from Cosmos DB?

No, you can't delete multiple documents in one go. Each document must be deleted separately. One possible solution would be to write a stored procedure and pass the list of document ids that you want to delete. In that stored procedure, you can loop through that list and delete individual documents.

How do you access the Cosmos DB in Azure portal?

Access Azure Cosmos DB ExplorerSign in to Azure portal. From All resources, find and navigate to your Azure Cosmos DB account, select Keys, and copy the Primary Connection String. Go to https://cosmos.azure.com/, paste the connection string and select Connect.

How do you debug a stored procedure in Cosmos database?

Azure Cosmos DB stored procedure is JS script running on the server, you can not debug it on your side. However , you can use console. log () to log some key steps in your stored procedure as below. Then use getScriptLog to get the output from stored procedure console.


2 Answers

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

like image 75
Rob Reagan Avatar answered Oct 08 '22 09:10

Rob Reagan


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.

like image 45
James Avatar answered Oct 08 '22 10:10

James