Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
How to delete several documents by ID in one operation using Elasticsearch Nest

I am building some abstraction functions for my application to call, which will hit elasticsearch through Nest. One of such functions is a Delete(string id) call, which is easy to accomplish. I have done this as follows:

public void Delete(string id)
{
    esClient.Delete(id);
}

Now let's say I want to do the same thing, but operate on several documents simultaneously. My original hunch was to do something like this:

public void Delete(IEnumerable<string> ids)
{
    esClient.DeleteMany(ids); // won't compile
}

As my comment states, doing this won't compile. What is the proper way of batch deleting documents by ID in Nest?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

To use esClient.DeleteMany(..) you have to pass collection of objects to delete.

var objectsToDelete = new List<YourType> {.. };
var bulkResponse = client.DeleteMany<YourType>(objectsToDelete);

You can get around this by using following code:

var ids = new List<string> {"1", "2", "3"};
var bulkResponse = client.DeleteMany<YourType>(ids.Select(x => new YourType { Id = x }));

Third option, use bulk delete:

var bulkResponse = client.Bulk(new BulkRequest
{
    Operations = ids.Select(x => new BulkDeleteOperation<YourType>(x)).Cast<IBulkOperation>().ToList()
});
over 4 years ago · Santiago Trujillo Report

0

I was working on a .NET client for ElasticSearch 5.x, and I was fortunate to have the following code running (as well as succeeding all unit tests) for bulk deletion using ID's:

  //IList<string> ids = ...

  var descriptor = new BulkDescriptor();

  foreach (var id in ids.Where(x => !string.IsNullOrWhiteSpace(x)))
    descriptor.Delete<T>(x => x
        .Id(id))
      .Refresh(Refresh.WaitFor);

  var response = await _client.BulkAsync(descriptor);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!