# How to add ttl for the documents in an index

**URL:** <https://forum.opensearch.org/t/how-to-add-ttl-for-the-documents-in-an-index/14723>\
**Category:** OpenSearch Client Libraries\
**Tags:** opensearch-java\
**Created:** [June 16, 2023, 11:50pm UTC](https://forum.opensearch.org/t/how-to-add-ttl-for-the-documents-in-an-index/14723 "2023-06-16T23:50:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fcheragh](https://avatars.discourse-cdn.com/v4/letter/f/f17d59/32.png) [@fcheragh](https://forum.opensearch.org/u/fcheragh)\
**Post date:** [June 16, 2023, 11:50pm UTC](https://forum.opensearch.org/t/how-to-add-ttl-for-the-documents-in-an-index/14723/1 "2023-06-16T23:50:30Z")

</div>

**Versions** (relevant - OpenSearch/Dashboard/Server OS/Browser):

**Describe the issue** :

I am trying to add a time for purging documents after a certain period. It looks like this has been removed! And it leaves me manually do it using delete\_by\_query and having a time of creation field in docs. I’m using open search java client. Is there anyone here knows why it is not there? Or I couldn’t find it!

**Configuration** :

**Relevant Logs or Screenshots** :

---

<div class="post-metadata">

**Author:** ![radu.gheorghe](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.opensearch.org/radu.gheorghe/32/3516_2.png) [@radu.gheorghe](https://forum.opensearch.org/u/radu.gheorghe)\
**Post date:** [June 29, 2023, 9:41am UTC](https://forum.opensearch.org/t/how-to-add-ttl-for-the-documents-in-an-index/14723/2 "2023-06-29T09:41:49Z")

</div>

Most people use ISM: [Index State Management - OpenSearch documentation](https://opensearch.org/docs/latest/im-plugin/ism/index/)

This requires you to roll over your indices frequently enough so that you drop indices older than X. It’s much more efficient than deleting by query, which triggers merges. More on merges here: [Solr: Optimize Is (Not) Bad for You – Video & Slides](https://sematext.com/blog/solr-optimize-is-not-bad-for-you-lucene-solr-revolution/)

---

<div class="post-metadata">

**Author:** ![fcheragh](https://avatars.discourse-cdn.com/v4/letter/f/f17d59/32.png) [@fcheragh](https://forum.opensearch.org/u/fcheragh)\
**Post date:** [July 14, 2023, 1:58pm UTC](https://forum.opensearch.org/t/how-to-add-ttl-for-the-documents-in-an-index/14723/3 "2023-07-14T13:58:41Z")

</div>

Thanks for the response. With IMS guide I need to create a policy first and then add it to the indices. I found no example on the internet of how to create and add policies using Java rest client.

The most comprehensive set of examples are listed here but there is no mention of policy. [https://github.com/opensearch-project/opensearch-java/blob/main/USER\_GUIDE.md](https://github.com/opensearch-project/opensearch-java/blob/main/USER_GUIDE.md)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/mauve_hedgehog/original/2X/3/33547ea01a5b12dcca2958411d3edd97ae2ea8c1.png) [@system](https://forum.opensearch.org/u/system)\
**Post date:** [September 12, 2023, 1:58pm UTC](https://forum.opensearch.org/t/how-to-add-ttl-for-the-documents-in-an-index/14723/4 "2023-09-12T13:58:53Z")

</div>

Closed on September 12, 2023, 1:58pm UTC

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