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.
Elasticsearch. delete_by_query method can be used for deleting documents matching a query.
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.
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.
Deleting an index deletes its documents, shards, and metadata. It does not delete related Kibana components, such as data views, visualizations, or dashboards.
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": {}}})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With