【问题标题】:How to clear a Cosmos DB database or delete all items using Azure portal如何使用 Azure 门户清除 Cosmos DB 数据库或删除所有项目
【发布时间】:2019-08-17 20:28:03
【问题描述】:

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

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

我发现 Cosmos DB 不允许使用 DELETE 代替 SELECT:https://stackoverflow.com/a/48339202/1198404,所以我应该这样做:

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

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

【问题讨论】:

  • 您要清除数据库(包括数据库的所有集合),还是清除数据库中的集合?
  • @RobReagan 我用随机值进行了测试。现在我将开始真正使用它(从测试转移到生产),所以我需要删除存储在数据库中的所有数据以删除所有内容,因此数据库是干净的。我在回答你的问题吗?我不确定这是否意味着清除数据库或只是一个集合!你能给我一些光吗? :)

标签: azure azure-cosmosdb azureportal azure-cosmosdb-sqlapi


【解决方案1】:

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

或者,这可能是一个更可行的选择,只是删除和重新创建容器。

【讨论】:

  • 不幸的是,这不起作用。将 TTL 设置为 0,它将在下次刷新时显示 0 个项目。但是,如果之后将 TTL 设置为 Off,则所有项目在下次刷新时都可见。
  • @widged 您在此之后等待多长时间以验证数据未被删除?还有,有多少记录?除非有什么改变,否则这确实有效,我有一点没用过 Cosmos。
  • 我最终手动删除了它们。他们被从视野中移除。我回来30分钟。之后。我所有的东西都回来了。所以,这可能是由于一个临时错误。不幸的是,使用 Azure 门户,功能突然更改或突然出现错误是很常见的。
  • @widged 是的,听起来不太对,tbh 可以尝试设置为 1s,以防 0 出现奇怪的行为。
  • 尝试过(显然),但本周 Cosmos 出现了更多新的/异常行为。他们似乎正在对 ATM 进行很多更改。最好等一个月再检查。
【解决方案2】:

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

由于您的连接字符串的范围是数据库级别,因此我快速清除 Container 中所有项目的方法是在数据库中删除并重新创建 Container。

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

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

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

【讨论】:

  • 感谢您的澄清!那么如何在 Azure 中做到这一点? :)
  • @chelder 我已经添加了有关如何执行此操作的信息。
  • 那么点击删除安全吗?我的意思是,下次应用服务尝试注册新用户时,它会像没有被删除一样正常存储在数据库中吗?谢谢! d.pr/i/V8B5xA
  • 就我而言,删除容器后,我必须再次将我们的应用服务发布到 Azure 以再次创建容器。
  • @chelder 删除后,您应该可以在 Azure 门户中重新创建容器。
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

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

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

    【讨论】:

    • 存储过程的范围是一个分区。你不能用这种方式清除一个集合。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    相关资源
    最近更新 更多