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

6

18

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 the usage of DELETE instead of SELECT: https://mcmap.net/q/740689/-delete-sql-for-cosmosdb-duplicate, so I should do something like:

SELECT * FROM c DELETE c
SELECT * FROM c DELETE *

But none of my attempts worked.

Lukin answered 17/8, 2019 at 20:28 Comment(2)
Do you want to clear a database (which includes all of the database's collections), or clear a collection within a database?Berlioz
@RobReagan I've testing with random values. Now I'm going to begin to use it for real (move from testing to production), so I need to delete all the data stored in database to remove everything, so the database is clean. Am I answering your question? I'm not sure if that means to clear the database or just a collection! May you give some light to me? :)Lukin
B
10

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

Berlioz answered 18/8, 2019 at 13:17 Comment(5)
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/V8B5xALukin
In my case, after deleting the container I had to publish to Azure our App Service again to create again the container.Lukin
@Lukin You should be able to recreate the Container right there within the Azure Portal after you delete it.Berlioz
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 todayBuoyage
It is asking for container id. Where can I find it?Drona
S
17

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.

Stannite answered 17/8, 2019 at 22:33 Comment(7)
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.Earhart
@Earhart 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.Stannite
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.Earhart
@Earhart yeah doesn't sound right tbh could try setting to 1s incase it's odd behaviour with 0.Stannite
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.Earhart
Whew, life saver! I experienced the same issue where after I turned the TTL off the items came back, but it seems like that's because the TTL deletion is a background task that takes a while. Wait for a while after turning on TTL to 1 and then check it out again.Clearsighted
I found for 7000 records it would not successfully remove them in 30m, so I left them over the weekend late friday-early monday, and that did the trick.Integrity
B
10

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

Berlioz answered 18/8, 2019 at 13:17 Comment(5)
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/V8B5xALukin
In my case, after deleting the container I had to publish to Azure our App Service again to create again the container.Lukin
@Lukin You should be able to recreate the Container right there within the Azure Portal after you delete it.Berlioz
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 todayBuoyage
It is asking for container id. Where can I find it?Drona
P
1

You can add a delete stored procedure to execute delete completely.

function bulkDeleteSproc(query) {
    var collection = getContext().getCollection();
    var collectionLink = collection.getSelfLink();
    var response = getContext().getResponse();
    var responseBody = {
        deleted: 0,
        continuation: true       
    };
    query='SELECT * FROM root r';

    // Validate input.
    if (!query) throw new Error("The query is undefined or null.");

    tryQueryAndDelete();


    function tryQueryAndDelete(continuation) {
        
        var requestOptions = {continuation: continuation};
        console.log(requestOptions);
        var isAccepted = collection.queryDocuments(collectionLink, query, requestOptions, function (err, retrievedDocs, responseOptions) {
            if (err) throw err;
              
            if (retrievedDocs.length > 0) {
                // Begin deleting documents as soon as documents are returned form the query results.
                // tryDelete() resumes querying after deleting; no need to page through continuation tokens.
                //  - this is to prioritize writes over reads given timeout constraints.
                tryDelete(retrievedDocs);
            } else if (responseOptions.continuation) {
                // Else if the query came back empty, but with a continuation token; repeat the query w/ the token.
                tryQueryAndDelete(responseOptions.continuation);
            } else {
                // Else if there are no more documents and no continuation token - we are finished deleting documents.
                responseBody.continuation = false;
                response.setBody(responseBody);
            }
        });

        // If we hit execution bounds - return continuation: true.
        if (!isAccepted) {
            console.log("tryquerydelete not accepted");
            response.setBody(responseBody);
        }
    }

    // Recursively deletes documents passed in as an array argument.
    // Attempts to query for more on empty array.
    function tryDelete(documents) {
        if (documents.length > 0) {
            // Delete the first document in the array.
            var isAccepted = collection.deleteDocument(documents[0]._self, {}, function (err, responseOptions) {
                if (err) throw err;

                responseBody.deleted++;
               console.log("hi");
                documents.shift();
               
                // Delete the next document in the array.
                tryDelete(documents);
                console.log(isAccepted);
            });

            // If we hit execution bounds - return continuation: true.
            if (!isAccepted) {
                console.log("trydelete not accepted");
                response.setBody(responseBody);
            }
        } else {
            // If the document array is empty, query for more documents.
            tryQueryAndDelete();
        }
    }
}
Postbellum answered 3/9, 2019 at 14:47 Comment(1)
Stored procedures are scoped to a partition. You can't just clear out a collection this way.Dayna
B
0

You can only bulk delete with BulkExecutor and not from the portal, you can only delete one item at a time from the Portal.

I would handle environment setup differently. I suggest you create separate resource group for each environment or at least create another collection for production. Regarding resource group solution to keep cost down, just tear down the test environment when not in use.

Berthoud answered 17/8, 2019 at 22:30 Comment(0)
W
0

To delete all the items using Azure portal it is best to just delete the container which contains the items.

Click on the 3 dots

enter image description here

and delete the container

enter image description here

Whin answered 25/7, 2023 at 20:17 Comment(0)
D
0

As Rob Reagan's answer already clarified, there is a difference between clearing all items from a container and clearing all items from a database. However, deleting the whole container to remove all items could lead to problems in certain scenarios. Often, the containers are defined and created via infrastructure as code (IaC) like Terraform or directly in code via the Cosmos client. When you delete the container, you must ensure that you create it with the same configuration again or rerun the IaC. If a Cosmos client is running when you delete a container (e.g., in a deployed service), the client would return errors regarding the existence of the container (e.g. 404 NotFound) while the container is absent.

Since for now there is no way to delete all items from a container in the Azure portal, you could use the already mentioned workarounds.

  • James's answer: Set the TTL of the container.
  • ArunkumaarCN's answer: Using stored procedures.

Another way would be to use an external tool that leverages the Cosmos client to delete all existing container items. Since I faced the same problem and didn't come across such a tool, I built my own Visual Studio Code Extension. If you like, try it out for yourself and install it from the marketplace. You can also clone the repository and modify it to match your needs.

Daylong answered 12/5 at 12:12 Comment(1)
there are many links in answer. adding a link is not recommended.Pendentive

© 2022 - 2024 — McMap. All rights reserved.