# How can we delete an index based on a scheduler using JAVA API's?

**URL:** <https://discuss.elastic.co/t/how-can-we-delete-an-index-based-on-a-scheduler-using-java-apis/292396>\
**Category:** Elasticsearch\
**Created:** [December 19, 2021, 12:57pm UTC](https://discuss.elastic.co/t/how-can-we-delete-an-index-based-on-a-scheduler-using-java-apis/292396 "2021-12-19T12:57:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![yash\_196](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yash_196/32/99273_2.png) [@yash\_196](https://discuss.elastic.co/u/yash_196)\
**Post date:** [December 19, 2021, 12:57pm UTC](https://discuss.elastic.co/t/how-can-we-delete-an-index-based-on-a-scheduler-using-java-apis/292396/1 "2021-12-19T12:57:44Z")

</div>

Can anyone help me with a java code that can delete index based on a schedule?

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [December 19, 2021, 4:25pm UTC](https://discuss.elastic.co/t/how-can-we-delete-an-index-based-on-a-scheduler-using-java-apis/292396/2 "2021-12-19T16:25:56Z")

</div>

Hi @yash_196 Welcome to the community.

Have you perhaps considered Index Lifecycle Management which can do this automatically and is already a built in feature?

> **[ILM: Manage the index lifecycle | Elasticsearch Guide \[7.16\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/index-lifecycle-management.html)**

---

<div class="post-metadata">

**Author:** ![yash\_196](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yash_196/32/99273_2.png) [@yash\_196](https://discuss.elastic.co/u/yash_196)\
**Post date:** [December 20, 2021, 5:24am UTC](https://discuss.elastic.co/t/how-can-we-delete-an-index-based-on-a-scheduler-using-java-apis/292396/3 "2021-12-20T05:24:15Z")

</div>

Hi @stephenb , thanks for the answer, ILM is a good solution for it. I have one more query: Is there any way to delete data within an index based on some search pattern/query which can be scheduled to run for a time period?

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [December 20, 2021, 5:36am UTC](https://discuss.elastic.co/t/how-can-we-delete-an-index-based-on-a-scheduler-using-java-apis/292396/4 "2021-12-20T05:36:51Z")

</div>

ILM is not query based it is time based.

You would have to look up delete by query which is not a extremely efficient method and you would have to schedule it yourself.

---

<div class="post-metadata">

**Author:** ![yash\_196](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yash_196/32/99273_2.png) [@yash\_196](https://discuss.elastic.co/u/yash_196)\
**Post date:** [December 20, 2021, 5:54am UTC](https://discuss.elastic.co/t/how-can-we-delete-an-index-based-on-a-scheduler-using-java-apis/292396/5 "2021-12-20T05:54:17Z")

</div>

is there any other way to delete based on a schedule? Is Watcher API helpful for these scenarios? To trigger based on a schedule and delete data from an index?

---

<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:** [January 17, 2022, 5:54am UTC](https://discuss.elastic.co/t/how-can-we-delete-an-index-based-on-a-scheduler-using-java-apis/292396/6 "2022-01-17T05:54:35Z")

</div>

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