90

I have Python 2.7 and Elasticsearch 2.1.1. I used the following to delete an index:

es.delete(index='researchtest', doc_type='test')

but this gives me this message:

return func(*args, params=params, **kwargs)
TypeError: delete() takes at least 4 arguments (4 given)

I also tried this technique:

es.delete_by_query(
    index='researchtest', 
    doc_type='test',
    body='{"query":{"match_all":{}}}'
)

but I get this message:

AttributeError: 'Elasticsearch' object has no attribute 'delete_by_query'

Has the API changed for 2.1.1 for python?

https://elasticsearch-py.readthedocs.org/en/master/api.html#elasticsearch.client.IndicesClient.delete

5 Answers 5

173

For ES 8+ use:

from elasticsearch import Elasticsearch
es = Elasticsearch()

es.options(ignore_status=[400,404]).indices.delete(index='test-index')

For older versions use this notation:

from elasticsearch import Elasticsearch
es = Elasticsearch()

es.indices.delete(index='test-index', ignore=[400, 404])
Sign up to request clarification or add additional context in comments.

3 Comments

is the ignore useful?
@LukAron it is if you are trying to delete a non existing index
This command returns a deprecation warning message for ElasticSearch python client 8+. So, it's alternative for ES 8+ is: es.options(ignore_status=[400,404]).indices.delete(index='test-index')
11

If you have a document object (model) and you're using elasticsearch-dsl, specially with Python-3.X you can directly call the delete method of your model's _index attribute.

ClassName._index.delete()

Also as it's stated in documentation:

The _index attribute is also home to the load_mappings method which will update the mapping on the Index from elasticsearch. This is very useful if you use dynamic mappings and want the class to be aware of those fields (for example if you wish the Date fields to be properly (de)serialized):

Post._index.load_mappings()

Comments

2

Since passing transport options in the API method is deprecated in the Elasticsearch python client 8+, the way to specify HTTP status codes that should be ignored (e.g. to prevent errors in case the target index does not exist) would be by using Elasticsearch.options() now:

from elasticsearch import Elasticsearch
es = Elasticsearch()

es.options(ignore_status=[400,404]).indices.delete(index='test-index')

(see documentation).

Comments

2

If you're using elasticsearch-dsl, use

from elasticsearch_dsl import Index

index = Index('test-index')
index.delete(ignore=[400, 404])

Comments

0

If you are using old version ES 8+:

from elasticsearch import Elasticsearch
es = Elasticsearch(http://localhost:9200)

# Delete
es.indices.delete(index='name_index', ignore=[400, 404])

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.