Loading...

Delete multiple documents from an Elasticsearch Index

:heavy_exclamation_mark: This post is older than a year. Consider some information might not be accurate anymore. :heavy_exclamation_mark:

This post demonstrates how to delete documents from an Index in Elasticsearch, that meet you search criteria of a query. You may have situations, that you are reporting to a wrong index. Therefore this post gives you an solution how you can clean up your index.

Following example deletes all documents from the default index, that has the type books.

vinh@cinhtau:/var/elasticsearch> curl -XDELETE 'http://localhost:9200/logstash-2016.02.10/_query' -d '
{
  "query": {
    "match": {
      "type": {
        "query": "books",
        "type": "phrase"
      }
    }
  }
}'
{"took":69645,"timed_out":false,"_indices":{"_all":{"found":176720,"deleted":176720,"missing":0,"failed":0},"logstash-2016.02.10":{"found":176720,"deleted":176720,"missing":0,"failed":0}},..

Keep in mind that, elasticsearch running in a cluster, has no problems with deleting multiple documents. In the past the API was crashing a single running instance of elasticsearch v1.x. In Elasticsearch 2 this functionality was reworked and outsource from the elasticsearch core. The Delete by query is in Elasticsearch 2.2 supported by the delete-by-query plugin and much more mature. To check for plugins just get the node information and check for the plugin. If it is installed it will be listed in the result.

Please remember the terms for blog comments.