3

On Azure Cosmos DB portal, Data Explorer doesn't allow deletion of more than one document.

enter image description here

Is it possible to delete multiple documents in one go?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Pingpong
  • 7,681
  • 21
  • 83
  • 209

3 Answers3

2

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

TylerH
  • 20,799
  • 66
  • 75
  • 101
Agrawal Shraddha
  • 734
  • 1
  • 5
  • 18
0

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.

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • I knew that, I want to avoid coding if possible. – Pingpong Jun 12 '19 at 15:15
  • 1
    If you're looking for a tool to delete multiple documents, do take a look at Cerebrata Cerulean (https://www.cerebrata.com/products/cerulean). It lets you select multiple documents and delete them in one go (behind the scenes, it deletes each document separately though). [Full Disclosure: My company is behind this tool] – Gaurav Mantri Jun 12 '19 at 15:21
  • Is it free to use? – Pingpong Jun 12 '19 at 15:57
  • Yes. It is a commercial software but we're offering a free 1 year subscription to all of our users so at least for an year you can use it for free. – Gaurav Mantri Jun 12 '19 at 16:02
  • is the software open source? My concern is that I have to provide the login credential of my work account. – Pingpong Jun 12 '19 at 18:39
  • 1
    @Pingpong This really isn't the place to have a discussion on how to get a product for free. – David Makogon Jun 29 '19 at 03:34
0

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; }
}
sveinungf
  • 841
  • 1
  • 12
  • 22