织梦CMS - 轻松建站从此开始!

abg欧博官网|登陆|游戏|

当前位置: abg欧博官网|登陆|游戏| > abg欧博 > 文章页

Delete items(documents) in bulk from Cosmos DB co

时间:2025-09-02 16:32来源: 作者:admin 点击: 7 次
I have at around 70,000 documents in cosmos db collection. I am using following stored procedure to delete items(documents) in bulk. But when I am e

I have at around 70,000 documents in cosmos db collection. I am using following stored procedure to delete items(documents) in bulk.

But when I am executing this stored procedure it is deleted only 500 documents. How Can I delete all items(documents) in one shot?

This is my delete query:

SELECT COUNT(1) FROM c WHERE c.UserUUId=null

Stored Procedure:

function bulkDeleteSproc(query) { var collection = getContext().getCollection(); var collectionLink = collection.getSelfLink(); var response = getContext().getResponse(); var responseBody = { deleted: 0, continuation: true }; // Validate input. if (!query) throw new Error("The query is undefined or null."); tryQueryAndDelete(); // Recursively runs the query w/ support for continuation tokens. // Calls tryDelete(documents) as soon as the query returns documents. function tryQueryAndDelete(continuation) { var requestOptions = {continuation: continuation}; 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) { 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++; documents.shift(); // Delete the next document in the array. tryDelete(documents); }); // If we hit execution bounds - return continuation: true. if (!isAccepted) { response.setBody(responseBody); } } else { // If the document array is empty, query for more documents. tryQueryAndDelete(); } } }

Here is the output after executing the store procedure:

enter image description here

(责任编辑:)
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:
发布者资料
查看详细资料 发送留言 加为好友 用户等级: 注册时间:2025-09-18 16:09 最后登录:2025-09-18 16:09
栏目列表
推荐内容