Learn how to Delete Document in ElasticSearch. delete by id, delete by name, delete by matching criteria, etc.
1. Delete document in Elasticsearch
You can delete using cURL
or any rest client that open source enthusiasts have created for Elasticsearch.
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?
2. 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.
3. Delete delete document in Elasticsearch by CURL
Delete by type by leaving documentID in the url.
curl -XDELETE localhost:9200/location/restro
OR
4. 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" : {} } }'
5. Delete document by documentId Using Rest Client
Service Input
- Service URL: http://localhost:9200/myindex/mytype/AVc7yauYQTyMXgOVj2NC
- Http method : Delete
- 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 }
6. Reference
Visit elastic.co for more details.
I hope you enjoyed this post to delete document in elasticSearch, visit ElasticSearch tutorial for more details.
Happy Learning! 🙂
Pingback: Kiran