Search code examples
pythonpython-2.7elasticsearch

How to delete an Elasticsearch Index using Python?


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


Solution

  • 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])