如何使用 Azure 门户清除 Cosmos DB 数据库或删除所有项目

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

如果转到 https://portal.azure.com,打开我们的 Azure Cosmos DB 帐户 (1) --> 数据资源管理器 (2) --> 单击用户 (3) --> 单击新建 SQL 查询:

Azure 将打开一个文本框以输入查询:

我发现 Cosmos DB 不允许改用 DELETE SELECT:,所以我应该这样做:

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

但我的任何尝试都奏效了。

您只能使用 BulkExecutor 进行批量删除,而不能从门户中删除,一次只能从门户中删除一项。

我会以不同的方式处理环境设置。我建议您为每个环境创建单独的资源组,或者至少为生产创建另一个集合。对于降低成本的资源组解决方案,只需在不使用时拆除测试环境。

一种选择是在该特定容器上将 TTL 设置为 0,具体取决于记录的数量,但可能需要一些时间。

或者,这可能是一个更可行的选择,就是简单地删除并重新创建容器。

Cosmos DB 数据库可以包含零个、一个或多个容器。容器存储项目。层次结构描述为 here。我假设您想清除所有项目的容器。

由于您的连接字符串被限制在数据库级别,我快速清除容器中所有项目的方法是在数据库中删除并重新创建容器。

要在 Azure 门户中删除容器,请执行以下操作:

  1. 在门户的左侧菜单中,选择所有资源 -> 然后选择您的 Cosmos DB 资源以调出 Cosmos DB 管理边栏选项卡。
  2. 选择数据资源管理器。您将看到您的数据库以及在其数据库下方列出的每个容器。
  3. 选择要删除的容器。突出显示 Container 的菜单项后,单击 Container 名称右侧的 ...。这将有一个弹出菜单,您可以在其中选择删除容器。

例如,如果容器名称是用户:

您可以添加一个删除存储过程来执行完全删除。

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();
        }
    }
}