How to Delete Elasticsearch Data Records by DSL-Query

by Shahid Ashraf

In this blog, we will see how we can delete data on elastic search index. Usually each of us have used the SQL query to delete certain rows of data from RDBMS table. The queries which we are familiar with are:

drop table twitter;

delete from twitter;

delete from twitter where tweet=”how to delete” and  tweetby_id =1;

The third query deletes rows based on the condition,  second deletes all the rows in the table and first deletes the table. Similarly, we can do the same on elastic search indexes.  Delete by query has been removed from elasticsearch api and is currently available as a plugin. In the following steps, we will see how we can achieve the above use cases on elasticsearch REST API calls. The delete-by-query plugin adds support for deleting all of documents (from one or more indices) which match the specified query. It is a replacement for problematic delete-by-query functionality which has been removed from Elasticsearch core as of Elasticsearch’s 2.0.0-beta1 release.

Step 1 Install the delete_by_query plugin

    sudo bin/plugin install delete-by-query

The plugin must be installed on every node in the cluster, and each node must be restarted after installation.

Step 2: Deletion of data

a) Deleting the entire index

This can be done without installing delete-by-query plugin,

          curl -XDELETE 'http://localhost:9200/twitter/tweet'

b) Deleting all the rows

 class="programlisting prettyprint lang-js prettyprinted">
      curl -XDELETE 'localhost:9200/twitter/tweet/_query?pretty' -d'
          "query": { 
          "match_all": {}

c) Delete the specific rows based on condition,

curl -XDELETE 'localhost:9200/twitter/tweet/_query?pretty' -d'
       "query": { 
           'must': [
           {'term': {'tweeted': "how to delete"}},
           {'term': {'tweetby_id': 1}}]

d) Delete the specified document directly,

    curl -XDELETE localhost:9200/index/type/documentID

So, that was easy, wasn’t it? Try it and let me know if you have any queries. For more information on how to delete data from elasticsearch using DSL queries, you can refer this link.




Leave a Reply

Your email address will not be published. Required fields are marked *

Tools & Practices

Tools and Technologies we use at Applied

Contact us now

Popular Posts