Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete all documents from elasticsearch index in Python 3.x

How can I delete all documents in Elasticsearch from index without deleting index itself?

from elasticsearch import Elasticsearch
es = Elasticsearch("http://elasticsearch.internal:9200")

Elasticsearch.delete(es, index="index")

response

TypeError: delete() missing 1 required positional argument: 'id'

Is there option like truncate table in sql. I know that I can loop all ids and delete each of them but maybe there is some magic option with wildcard for example.

like image 593
Hubert Dudek Avatar asked Sep 03 '19 20:09

Hubert Dudek


People also ask

How do I delete all files in Elasticsearch index in Python?

Elasticsearch. delete_by_query method can be used for deleting documents matching a query.

How do I delete all index elastic files?

We can delete all the documents from the index using _delete_by_query. when we pass “match_all”:{} then it will match all the documents so _delete_by_query will delete all the documents from the index.

How do I get rid of elastic search documents?

You use DELETE to remove a document from an index. You must specify the index name and document ID. You cannot send deletion requests directly to a data stream. To delete a document in a data stream, you must target the backing index containing the document.

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.


1 Answers

Elasticsearch.delete_by_query method can be used for deleting documents matching a query.

https://elasticsearch-py.readthedocs.io/en/master/api.html#elasticsearch.Elasticsearch.delete_by_query

indices = ['index1', 'index2', 'other-index-names']
es.delete_by_query(index=indices, body={"query": {"match_all": {}}})
like image 196
Oluwafemi Sule Avatar answered Oct 30 '22 21:10

Oluwafemi Sule