# How to delete all the elasticsearch indices, that start with a certain string, based on date ranges

**URL:** https://discuss.elastic.co/t/how-to-delete-all-the-elasticsearch-indices-that-start-with-a-certain-string-based-on-date-ranges/283063
**Category:** Elasticsearch
**Created:** [September 1, 2021, 3:16pm UTC](https://discuss.elastic.co/t/how-to-delete-all-the-elasticsearch-indices-that-start-with-a-certain-string-based-on-date-ranges/283063 "2021-09-01T15:16:01Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![JaswantG](https://avatars.discourse-cdn.com/v4/letter/j/d78d45/32.png) [@JaswantG](https://discuss.elastic.co/u/JaswantG)
#### Post date: [September 1, 2021, 3:16pm UTC](https://discuss.elastic.co/t/how-to-delete-all-the-elasticsearch-indices-that-start-with-a-certain-string-based-on-date-ranges/283063/1 "2021-09-01T15:16:01Z")

</div>

I am using elasticsearch 2.2.0. I have a requirement wherein I have to delete certain indices, which are created on a day-to-day basis, based on two different dates.  
(format - **abc-xyz-yyyy-mm-dd** )  
For e.g. - There are 100 indices which are created between 2021-01-01 and 2021-04-10. How do I delete the indices that were created between 2021-02-01 and 2021-02-21? I should only delete the indices that begin with abc-xyz.  
Can anyone help me on this? I am relatively new to ES.

---

<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: [September 1, 2021, 10:04pm UTC](https://discuss.elastic.co/t/how-to-delete-all-the-elasticsearch-indices-that-start-with-a-certain-string-based-on-date-ranges/283063/2 "2021-09-01T22:04:55Z")

</div>

Elasticsearch Curator might be able to this for you - [Curator Reference [8.0] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/curator/current/index.html).

> [@JaswantG](#):
>
> I am using elasticsearch 2.2.0

You should really upgrade, urgently.  
2.X has been [EOL](https://www.elastic.co/support/eol) for quite a long, long time, and latest is 7.14.

---

<div class="post-metadata">

### Author: ![JaswantG](https://avatars.discourse-cdn.com/v4/letter/j/d78d45/32.png) [@JaswantG](https://discuss.elastic.co/u/JaswantG)
#### Post date: [September 2, 2021, 5:18am UTC](https://discuss.elastic.co/t/how-to-delete-all-the-elasticsearch-indices-that-start-with-a-certain-string-based-on-date-ranges/283063/3 "2021-09-02T05:18:44Z")

</div>

Is there any way to delete the indices for my requirement using the delete API?

---

<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: [September 2, 2021, 5:27am UTC](https://discuss.elastic.co/t/how-to-delete-all-the-elasticsearch-indices-that-start-with-a-certain-string-based-on-date-ranges/283063/4 "2021-09-02T05:27:01Z")

</div>

Only if you want to do something like `curl -H "Content-Type: application/json" -XDELETE 0:9200/abc-xyz-2021-02-*`.

Otherwise just whip up a bash for loop with a counter to cover the range you need.

---

<div class="post-metadata">

### Author: ![JaswantG](https://avatars.discourse-cdn.com/v4/letter/j/d78d45/32.png) [@JaswantG](https://discuss.elastic.co/u/JaswantG)
#### Post date: [September 2, 2021, 5:53am UTC](https://discuss.elastic.co/t/how-to-delete-all-the-elasticsearch-indices-that-start-with-a-certain-string-based-on-date-ranges/283063/5 "2021-09-02T05:53:00Z")

</div>

Thanks Mark, I used the for loop procedure and it worked, but is there any way to check if an index exists and only then delete it in the for loop?

---

<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: [September 2, 2021, 5:53am UTC](https://discuss.elastic.co/t/how-to-delete-all-the-elasticsearch-indices-that-start-with-a-certain-string-based-on-date-ranges/283063/6 "2021-09-02T05:53:53Z")

</div>

If you script that yourself, yes.

But if a specific index doesn't exist it cannot be deleted and Elasticsearch will reject the request.

---

<div class="post-metadata">

### Author: ![JaswantG](https://avatars.discourse-cdn.com/v4/letter/j/d78d45/32.png) [@JaswantG](https://discuss.elastic.co/u/JaswantG)
#### Post date: [September 2, 2021, 5:55am UTC](https://discuss.elastic.co/t/how-to-delete-all-the-elasticsearch-indices-that-start-with-a-certain-string-based-on-date-ranges/283063/7 "2021-09-02T05:55:04Z")

</div>

could you please tell me how to check if an index exists?

---

<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: [September 2, 2021, 6:03am UTC](https://discuss.elastic.co/t/how-to-delete-all-the-elasticsearch-indices-that-start-with-a-certain-string-based-on-date-ranges/283063/8 "2021-09-02T06:03:05Z")

</div>

[Exists API | Elasticsearch Guide [7.14] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/7.14/indices-exists.html) is the best.

---

<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: [September 30, 2021, 6:03am UTC](https://discuss.elastic.co/t/how-to-delete-all-the-elasticsearch-indices-that-start-with-a-certain-string-based-on-date-ranges/283063/9 "2021-09-30T06:03:45Z")

</div>

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