# Delete\_by\_query not deleting the indices

**URL:** <https://discuss.elastic.co/t/delete-by-query-not-deleting-the-indices/304500>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [May 11, 2022, 7:13pm UTC](https://discuss.elastic.co/t/delete-by-query-not-deleting-the-indices/304500 "2022-05-11T19:13:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sam1231](https://avatars.discourse-cdn.com/v4/letter/s/fbc32d/32.png) [@Sam1231](https://discuss.elastic.co/u/Sam1231)\
**Post date:** [May 11, 2022, 7:13pm UTC](https://discuss.elastic.co/t/delete-by-query-not-deleting-the-indices/304500/1 "2022-05-11T19:13:15Z")

</div>

hi,

i have a python program which needs to delete the indices after 20 days of creation.

i have used delete\_by\_query but when i run the program, there is no error but also the indices are not getting deleted.  
can someone help me with this.

im running AWS based Elasticsearch version 6.4.5

below is the code snippet -

```auto
import datetime
from datetime import date
from elasticsearch import Elasticsearch
import logging
import sys

logging.basicConfig(level=logging.INFO)

class ESUtility(object):
    def __init__ (self):
        """
            Initializer
        """
        try:
            #please provide host and port applicable
            HOST = "abc.com"
            PORT = 80
            self. __ES_OBJ__ = Elasticsearch(host=HOST, port=PORT)
            logging.info("Connection established successfully")
        except Exception as e:
            error_message = "Exception while establishing connection to database" \
                        "{error}".format(error=str(e))
            logging.info(error_message)

    def delete_by_query(self,index_name,query):
        try:
            result = self. __ES_OBJ__.delete_by_query(index=index_name, body=query)
            #result = query
            return result
        except Exception as e:
            error_message = "Error while updating data" \
                        "{error}".format(error=str(e))
            logging.info(error_message)

class AutoCleanUp:
    @staticmethod
    def cleanup(no_of_days):
        try:
            tod = date.today()
            date_difference = datetime.timedelta(days=int(no_of_days))
            target_date = tod - date_difference
            #Please provide index name
            index_name = "test-logs-*"
            delete_query = {
                          "query": {
                            "bool": {
                              "must": [
                                {
                                  "range": {
                                    "date": {
                                      "lt": target_date
                                    }
                                  }
                                }
                              ]
                            }
                          }
                        }
            ESUtility().delete_by_query(index_name=index_name,
                                    query=delete_query)
            return ("Deleted Successfully")
        except Exception as e:
            logging.exception(str(e))
            raise Exception("Error while deleting data")

if __name__ == " __main__":
    AutoCleanUp.cleanup(sys.argv[1])

```

---

<div class="post-metadata">

**Author:** ![leandrojmp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leandrojmp/32/107231_2.png) [@leandrojmp](https://discuss.elastic.co/u/leandrojmp)\
**Post date:** [May 11, 2022, 8:40pm UTC](https://discuss.elastic.co/t/delete-by-query-not-deleting-the-indices/304500/2 "2022-05-11T20:40:55Z")

</div>

The `delete_by_query` API does not remove indices, only documents.

The best way to delete indices after a specified amount of time is using ILM, but since you are using the AWS Elasticsearch, and a pretty old version, you will need to use other tool, maybe [curator](https://www.elastic.co/guide/en/elasticsearch/client/curator/current/about.html) can help in your case.

---

<div class="post-metadata">

**Author:** ![Sam1231](https://avatars.discourse-cdn.com/v4/letter/s/fbc32d/32.png) [@Sam1231](https://discuss.elastic.co/u/Sam1231)\
**Post date:** [May 11, 2022, 8:56pm UTC](https://discuss.elastic.co/t/delete-by-query-not-deleting-the-indices/304500/3 "2022-05-11T20:56:00Z")

</div>

Thanks for the suggestion on using curator but i would also like to know which API needs to be used for deleting indices after certain number of days of index creation.

---

<div class="post-metadata">

**Author:** ![leandrojmp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leandrojmp/32/107231_2.png) [@leandrojmp](https://discuss.elastic.co/u/leandrojmp)\
**Post date:** [May 11, 2022, 10:02pm UTC](https://discuss.elastic.co/t/delete-by-query-not-deleting-the-indices/304500/4 "2022-05-11T22:02:03Z")

</div>

There isn't an API to do that.

In newer versions this is done using the ILM, Index Lifecycle Management, but this does not exist in the version and distribution that you are using, so you will need to use curator or build an script to do that.

You could write an script that would check the index creation date of the index and delete it if it matches your requirements.

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [May 11, 2022, 11:07pm UTC](https://discuss.elastic.co/t/delete-by-query-not-deleting-the-indices/304500/5 "2022-05-11T23:07:58Z")

</div>

> [@Sam1231](#):
>
> im running AWS based Elasticsearch version 6.4.5

6.4 is [EOL](https://www.elastic.co/support/eol) and no longer supported, you should upgrade.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 11, 2022, 11:07pm UTC](https://discuss.elastic.co/t/delete-by-query-not-deleting-the-indices/304500/6 "2022-05-11T23:07:58Z")

</div>

Elasticsearch version 6.4.5 is [EOL](https://www.elastic.co/support/eol) and no longer supported. Please upgrade ASAP.

(This is an automated response from your friendly Elastic bot. Please report this post if you have any suggestions or concerns :elasticheart: )

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 8, 2022, 11:08pm UTC](https://discuss.elastic.co/t/delete-by-query-not-deleting-the-indices/304500/7 "2022-06-08T23:08:29Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
