7

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.

2
  • Elasticsearch needs to be instantiated before using it. Commented Sep 3, 2019 at 20:41
  • @OluwafemiSule it is Commented Sep 3, 2019 at 20:44

2 Answers 2

15

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": {}}})
Sign up to request clarification or add additional context in comments.

Comments

0

The elasticsearch.Elasticsearch.delete method is not a static method and should be called using an instance of elasticache.Elasticsearch, and this way self will automatically be passed as an argument to the delete method when it is called.

Example:

from elasticsearch import Elasticsearch

es = Elasticsearch()

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

Secondly, as described by the elastic search documentation here, "You use DELETE to remove a document from an index. You must specify the index name and document ID." Therefore, you should provide it both.

Example:

doc_id = "my_document"
my_index = "index"
es.delete(id=doc_id, index=my_index)

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.