Jim G.
Jim G.

Reputation: 15365

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

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.

Upvotes: 19

Views: 10026

Answers (5)

Quoc Le
Quoc Le

Reputation: 416

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())
      );

Upvotes: 0

hross
hross

Reputation: 3671

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("*")))
);

Upvotes: 28

vishnu das
vishnu das

Reputation: 11

**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"));

Upvotes: 1

user3775217
user3775217

Reputation: 4803

$ 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

Upvotes: 1

JayTee
JayTee

Reputation: 1239

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

Upvotes: 4

Related Questions