Reputation: 8019
On Azure Cosmos DB portal, Data Explorer doesn't allow deletion of more than one document.
Is it possible to delete multiple documents in one go?
Upvotes: 3
Views: 12985
Reputation: 1091
I ended up creating a small C# console application with the Microsoft.Azure.Cosmos
NuGet package installed. It might not be the fastest way of deleting documents, but it gets the job done. It assumes using /id
as the partition key:
using Microsoft.Azure.Cosmos;
// Fill in these
const string connectionString "";
const string databaseName = "";
const string containerName = "";
const string query = ""; // For example "SELECT c.id FROM c WHERE c.EnqueuedTimeUtc >= '2023-05-11T15:00';
const int batchCount = 80; // Some limit to prevent getting "429 Too Many Requests".
var client = new CosmosClient(connectionString);
var container = client.GetContainer(databaseName, containerName);
var iterator = container.GetItemQueryIterator<MyDoc>(query, requestOptions: new QueryRequestOptions { MaxItemCount = batchCount });
while (iterator.HasMoreResults)
{
var response = await iterator.ReadNextAsync();
var deleteTasks = response.Select(doc => container.DeleteItemAsync<object>(doc.id, new PartitionKey(doc.id))).ToList();
await Task.WhenAll(deleteTasks);
Console.WriteLine($"{DateTime.Now:T} - Deleted {deleteTasks.Count} documents");
}
Console.WriteLine("Done!");
class MyDoc
{
public string id { get; set; }
}
Upvotes: 0
Reputation: 764
You cannot delete multiple documents, but you can use stored procedure to delete documents in one partition.
Please refer to this Q&A set for information on that: Azure CosmosDB: stored procedure delete documents based on query
Upvotes: 3
Reputation: 136366
No, you can't delete multiple documents in one go. Each document must be deleted separately.
One possible solution would be to write a stored procedure and pass the list of document ids that you want to delete. In that stored procedure, you can loop through that list and delete individual documents.
Upvotes: 1