How to Delete Document in Elastic Search?

Connect with

elastic search
When you get chance to delete either documents or type or index , do it very carefully. How to delete index in elastic search is a straight forward but do this very carefully specially in production environment. In this post I tried to demonstrate how to delete index and its indexed content in elastic search.

You can delete using cURL or visually using one of the many tools that open source enthusiasts have created for Elastic search.

By end of this post, you can learn following:

  • how to delete elastic document doc id by using cURL?
  • how to delete type of elastic by using cURL?
  • how to delete document by matching query using cURL?
  • how to delete document by documentID using Rest Client?

Delete Document by doc id using cURL

Format of curl query as follows:

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

for example:

curl -XDELETE localhost:9200/location/restro/1

Note: here , after port number (9200) , location string which is index name of elastic and restro is type, which comes under index name “location”. and “1” is the documentId all are in path parameter

You get reply from curl output as to whether this was successful or not.

Delete by type in Elastic by CURL

Delete by type by leaving documentID in the url.

curl -XDELETE localhost:9200/location/restro

OR

Delete Document by Matching Query Using CURL

You can delete by query with a match all , for example , here “location” is indexName and type name

curl -XDELETE 'http://localhost:9200/location/restro/_query' -d '{
    "query" : { 
        "match_all" : {}
    }
}'

Delete document by documentId Using Rest Client

Service Input

  1. Service URL: http://localhost:9200/myindex/mytype/AVc7yauYQTyMXgOVj2NC
  2. Http method : Delete
  3. Content-Type: application/json

Service Output

{
"acknowledged": true
}

Once deleted and you are trying to delete again, you should get error similar as follows:

{  
   "error":{  
      "root_cause":[  
         {  
            "type":"index_not_found_exception",
            "reason":"no such index",
            "index":"myindex",
            "resource.type":"index_or_alias",
            "resource.id":"myindex"
         }
      ],
      "type":"index_not_found_exception",
      "reason":"no such index",
      "index":"myindex",
      "resource.type":"index_or_alias",
      "resource.id":"myindex"
   },
   "status":404
}

Write your suggestions here to improve this post.


Connect with

1 thought on “How to Delete Document in Elastic Search?

  1. Pingback: Kiran

Leave a Reply

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