# Delete index after 1 day if empty and after n days if not empty

**URL:** https://discuss.elastic.co/t/delete-index-after-1-day-if-empty-and-after-n-days-if-not-empty/351741
**Category:** Elasticsearch
**Tags:** ilm-index-lifecycle-management
**Created:** [January 24, 2024, 4:37pm UTC](https://discuss.elastic.co/t/delete-index-after-1-day-if-empty-and-after-n-days-if-not-empty/351741 "2024-01-24T16:37:21Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![elastico23](https://avatars.discourse-cdn.com/v4/letter/e/2bfe46/32.png) [@elastico23](https://discuss.elastic.co/u/elastico23)
#### Post date: [January 24, 2024, 4:37pm UTC](https://discuss.elastic.co/t/delete-index-after-1-day-if-empty-and-after-n-days-if-not-empty/351741/1 "2024-01-24T16:37:21Z")

</div>

Hi, we use daily indices and currently use an ILM policy to delete indices after n days. However, we have some indices each day that don't end up having documents and would like to delete those after 1 day instead of keeping them around for the full n days.

Can we specify an ILM policy to delete an index after 1 day if it's empty but keep it for n days if it isn't empty?

Thanks!

---

<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: [January 24, 2024, 4:46pm UTC](https://discuss.elastic.co/t/delete-index-after-1-day-if-empty-and-after-n-days-if-not-empty/351741/2 "2024-01-24T16:46:15Z")

</div>

> [@elastico23](#):
>
> Can we specify an ILM policy to delete an index after 1 day if it's empty but keep it for n days if it isn't empty?

No, this is not possible, ILM works on entire indices, and for the DELETE phase it doesn't matter if they have documents or not, they will be deleted by age.

---

<div class="post-metadata">

### Author: ![elasticforme](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@elasticforme](https://discuss.elastic.co/u/elasticforme)
#### Post date: [January 25, 2024, 9:44am UTC](https://discuss.elastic.co/t/delete-index-after-1-day-if-empty-and-after-n-days-if-not-empty/351741/3 "2024-01-25T09:44:55Z")

</div>

write small bash script or python script.  
check count of record in index and if zero delete.

[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-count.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-count.html)

---

<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: [February 22, 2024, 9:45am UTC](https://discuss.elastic.co/t/delete-index-after-1-day-if-empty-and-after-n-days-if-not-empty/351741/4 "2024-02-22T09:45:51Z")

</div>

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