通过门户从Azure Cosmos DB删除所有/多个文档
是否可以通过azure门户、azure cosmos SQL查询或power shell脚本删除集合中的所有/多个可用文档?根据我的经验,删除所有文档的最快方法是将容器上的生存时间设置为1秒。这将删除所有文档。但请注意,此过程需要一些时间,因此如果您太快将“生存时间”设置为“无限”,尚未删除的文档将重新出现 您可以设置容器的缩放和设置下的生存时间:url-> 您还可以在容器中创建一个存储过程并运行该..url-> 存储过程:通过门户从Azure Cosmos DB删除所有/多个文档,azure,azure-cosmosdb,Azure,Azure Cosmosdb,是否可以通过azure门户、azure cosmos SQL查询或power shell脚本删除集合中的所有/多个可用文档?根据我的经验,删除所有文档的最快方法是将容器上的生存时间设置为1秒。这将删除所有文档。但请注意,此过程需要一些时间,因此如果您太快将“生存时间”设置为“无限”,尚未删除的文档将重新出现 您可以设置容器的缩放和设置下的生存时间:url-> 您还可以在容器中创建一个存储过程并运行该..url-> 存储过程: /** * A Cosmos DB stored procedure
/**
* A Cosmos DB stored procedure that bulk deletes documents for a given query.<br/>
* Note: You may need to execute this stored procedure multiple times (depending whether the stored procedure is able to delete every document within the execution timeout limit).
*
* @function
* @param {string} query - A query that provides the documents to be deleted (e.g. "SELECT c._self FROM c WHERE c.founded_year = 2008"). Note: For best performance, reduce the # of properties returned per document in the query to only what's required (e.g. prefer SELECT c._self over SELECT * )
* @returns {Object.<number, boolean>} Returns an object with the two properties:<br/>
* deleted - contains a count of documents deleted<br/>
* continuation - a boolean whether you should execute the stored procedure again (true if there are more documents to delete; false otherwise).
*/
function bulkDeleteStoredProcedure(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();
}
}
}
然后,您可以编写一个powershellscript来运行该存储过程
更新
我相信设置生存时间的另一个优点是它不需要花费任何RU,但是使用存储过程删除将是有益的。我将生存时间设置为1秒的可能重复项。从c运行SELECT*并看到没有文档返回。然后过了一会儿,再次运行查询,那里有大量文档。这正常吗?视情况而定。如果不到一秒钟前创建的所有文档都是。但是,如果您的意思是您已经结束了生存时间,重新选择并获得了许多您认为在生存时间之前已被删除的文档,那么请再次阅读此答案的第一段=简短的回答是,是的,这是正常的。@DavidKlempfner请参阅上面的评论。。忘记了@you对于那些好奇这个过程需要一些时间的人来说意味着什么。。。我只是在一小部分4k上做了这个。大约30分钟内什么也没发生,一分钟后一切都消失了。@Davidkendall先生我不确定,但我认为这取决于分配给集合的RU数量。