ElasticSearch and NEST: How do you purge all documents from an index?
Asked Answered
B

5

18

I know how to delete an entire ElasticSearch index, but how do you purge all documents from an index?

My Motivation: I'd like to have a "ReIndex" method that purges the entire contents of an index so that I can reload all documents.

ElasticSearch syntax would be helful. NEST syntax would be even better.

Beefeater answered 13/11, 2014 at 19:52 Comment(2)
DeleteByQuery is now deprecated. Any one got the latest syntax in Nest 2 versions?Peroneus
@Linoy.M.Kunjappan DeleteByQuery is no longer deprecated. As stated here "DBQ was originally deprecated and removed from Elasticsearch itself, then they brought it back with a new implementation."Sequestration
W
27

I was looking for something similar in Nest and I thought I'd put the syntax here for anyone looking:

var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(node);
var client = new ElasticClient(settings);

client.DeleteByQuery<ElasticsearchProject>(del => del
    .Query(q => q.QueryString(qs=>qs.Query("*")))
);
Wun answered 13/3, 2015 at 12:48 Comment(2)
This is a generic form that requires a T to be specified. What if I want to delete form all indices and all types?Sharper
@MathiasLykkegaardLorenzen Use dynamic instead of T.Dynamics
I
4

You can use a delete by query. This will delete all documents that match * i.e. everything.

curl -XDELETE localhost:9200/<indexname>/_query?q=*
  • Change localhost to the hostname that node is running on.

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-delete-by-query.html

Don't forget to optimize afterwards.

curl localhost:9200/<indexname>/_optimize
Instantaneous answered 13/11, 2014 at 19:58 Comment(1)
Link is dead. https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html looks like a good replacement.Sequestration
P
1

$ curl -XPOST localhost:9200/myindex/_optimize ....

The optimization process will clean all your softdeletes done by you by delete by query.

We are also facing a simillar problem where we deletes a lot of documents.Actually we move lot of documents from one index to other as we have sharded data by date. But as ES doesn't support moving of data from one index to other.

But optimization, is a costly operation as it consumes a lot of IO seeks. If you just want to do purge just for deletes I guess then you can utilize "only_expunge_deletes" flag to merge segments with deletes only.

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-optimize.html

Poltroonery answered 15/11, 2014 at 18:19 Comment(0)
M
1
**To delete all Records -**
client.DeleteByQuery<ElasticsearchProject>(del => del
            .Query(q => q.QueryString(qs=>qs.Query("*"))
        ));
**To delete index-**
client.DeleteIndex(d => d.Index("index_name"));
Mossberg answered 10/8, 2015 at 6:11 Comment(0)
C
0

Delete IndexName with ES v7.0.9. Use.AllIndices() or .Index("_all") instead .Index("IndexName") to delete all Index

await DeleteByQueryAsync<object>(q => q
          .Index("IndexName")
          .Query(rq => rq.MatchAll())
      );
Chirk answered 14/6, 2023 at 7:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.