# How to automatically delete documents older than 3 months

**URL:** <https://discuss.elastic.co/t/how-to-automatically-delete-documents-older-than-3-months/265851>\
**Category:** Elasticsearch\
**Created:** [March 1, 2021, 7:27pm UTC](https://discuss.elastic.co/t/how-to-automatically-delete-documents-older-than-3-months/265851 "2021-03-01T19:27:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kate\_yxy](https://avatars.discourse-cdn.com/v4/letter/k/e19b73/32.png) [@kate\_yxy](https://discuss.elastic.co/u/kate_yxy)\
**Post date:** [March 1, 2021, 7:27pm UTC](https://discuss.elastic.co/t/how-to-automatically-delete-documents-older-than-3-months/265851/1 "2021-03-01T19:27:30Z")

</div>

Hey,

I want to delete documents older than 3 months in my index. And I have read the doc here about using ILM to create time-based indices and apply ILM policies. However, this doesn't seem to fit our use case. We're using id we generate to write/update/read documents in the index. If we're to create time-based indices, we want to keep documents with a specific id unique across indices and this doesn't seem possible from what I learned.

Is there any other ways to automatically delete old documents that will fit my use case ?

---

<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:** [March 2, 2021, 2:09am UTC](https://discuss.elastic.co/t/how-to-automatically-delete-documents-older-than-3-months/265851/2 "2021-03-02T02:09:07Z")

</div>

Welcome to our community! 😃

No there's not, no. You will need to wrap something together with a delete by query.

> [@kate\_yxy](#):
>
> However, this doesn't seem to fit our use case. We're using id we generate to write/update/read documents in the index. If we're to create time-based indices, we want to keep documents with a specific id unique across indices and this doesn't seem possible from what I learned.

So you keep some data for longer than 3 months?

---

<div class="post-metadata">

**Author:** ![kate\_yxy](https://avatars.discourse-cdn.com/v4/letter/k/e19b73/32.png) [@kate\_yxy](https://discuss.elastic.co/u/kate_yxy)\
**Post date:** [March 2, 2021, 7:25pm UTC](https://discuss.elastic.co/t/how-to-automatically-delete-documents-older-than-3-months/265851/3 "2021-03-02T19:25:47Z")

</div>

Right now we have data older than 3 months. But we only need data within 3 months. For now we can do a manual delete-by-query, but in the future we want to automatically delete those old data. Is that possible ?

---

<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:** [March 2, 2021, 7:27pm UTC](https://discuss.elastic.co/t/how-to-automatically-delete-documents-older-than-3-months/265851/4 "2021-03-02T19:27:17Z")

</div>

Yes, use time based indices and then delete those when they are older than 3 months.

---

<div class="post-metadata">

**Author:** ![kate\_yxy](https://avatars.discourse-cdn.com/v4/letter/k/e19b73/32.png) [@kate\_yxy](https://discuss.elastic.co/u/kate_yxy)\
**Post date:** [March 3, 2021, 12:16am UTC](https://discuss.elastic.co/t/how-to-automatically-delete-documents-older-than-3-months/265851/5 "2021-03-03T00:16:24Z")

</div>

But we also need to satisfy that each document should have a unique id across indices.

---

<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:** [March 3, 2021, 12:16am UTC](https://discuss.elastic.co/t/how-to-automatically-delete-documents-older-than-3-months/265851/6 "2021-03-03T00:16:53Z")

</div>

Ok, not sure why that doesn't mean you can't use time based indices though.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [March 3, 2021, 6:05am UTC](https://discuss.elastic.co/t/how-to-automatically-delete-documents-older-than-3-months/265851/7 "2021-03-03T06:05:07Z")

</div>

If that is the case time based indices may not be right for your use case and you will need to implement a way to trigger a periodic delete by query yourself as Elasticsearch does not support this.

---

<div class="post-metadata">

**Author:** ![kate\_yxy](https://avatars.discourse-cdn.com/v4/letter/k/e19b73/32.png) [@kate\_yxy](https://discuss.elastic.co/u/kate_yxy)\
**Post date:** [March 9, 2021, 10:22pm UTC](https://discuss.elastic.co/t/how-to-automatically-delete-documents-older-than-3-months/265851/8 "2021-03-09T22:22:06Z")

</div>

For the trigger, I saw this watcher [Getting started with Watcher | Elasticsearch Reference [7.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-getting-started.html)  
Is watcher can be used to delete documents ?

---

<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:** [April 6, 2021, 10:22pm UTC](https://discuss.elastic.co/t/how-to-automatically-delete-documents-older-than-3-months/265851/9 "2021-04-06T22:22:09Z")

</div>

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