Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 656
Jim G. Avatar asked Nov 13 '14 19:11

Jim G.


People also ask

How do I remove all Elasticsearch index files?

2. Delete all documents from the index. We can delete all the documents from the index using _delete_by_query.

Does deleting index delete documents Elasticsearch?

Deleting an index deletes its documents, shards, and metadata. It does not delete related Kibana components, such as data views, visualizations, or dashboards.


2 Answers

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("*")))
);
like image 151
hross Avatar answered Sep 20 '22 09:09

hross


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
like image 29
JayTee Avatar answered Sep 17 '22 09:09

JayTee