# Data Retention

**URL:** <https://discuss.elastic.co/t/data-retention/81629>\
**Category:** Elasticsearch\
**Created:** [April 7, 2017, 3:00pm UTC](https://discuss.elastic.co/t/data-retention/81629 "2017-04-07T15:00:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![saad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/saad/32/17214_2.png) [@saad](https://discuss.elastic.co/u/saad)\
**Post date:** [April 7, 2017, 3:00pm UTC](https://discuss.elastic.co/t/data-retention/81629/1 "2017-04-07T15:00:46Z")

</div>

Hi,

How can I assign the data retention in elasticsearch for example 3 months.

Regards

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 7, 2017, 3:13pm UTC](https://discuss.elastic.co/t/data-retention/81629/2 "2017-04-07T15:13:29Z")

</div>

You can't but you can use curator to clean your indices on a regular basis.

---

<div class="post-metadata">

**Author:** ![theuntergeek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/theuntergeek/32/44961_2.png) [@theuntergeek](https://discuss.elastic.co/u/theuntergeek)\
**Post date:** [April 7, 2017, 3:13pm UTC](https://discuss.elastic.co/t/data-retention/81629/3 "2017-04-07T15:13:33Z")

</div>

Elasticsearch does not handle data retention on its own. You have to do it yourself, using a tool like [Elasticsearch Curator](https://www.elastic.co/guide/en/elasticsearch/client/curator/4.2/index.html) or manually use the API.

---

<div class="post-metadata">

**Author:** ![ZillaG](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zillag/32/10505_2.png) [@ZillaG](https://discuss.elastic.co/u/ZillaG)\
**Post date:** [April 7, 2017, 8:08pm UTC](https://discuss.elastic.co/t/data-retention/81629/4 "2017-04-07T20:08:11Z")

</div>

I wrote this script to manage my ELK indices. It's self-explanatory. It does assume that you create daily indices in the form of "logstash-\*-YYYY.mm.dd"

```
#######################################################################
# Script to manage ELK indices
# - Close week-old ELK indicies
# - Delete 90-day-old ELK indicies
# To run daily in crontab, say at 1:00am
# 0 1 * * * /root/elk/close_last_week_to_date_elk_indices.sh
#######################################################################
CERT="/etc/elasticsearch/chain-ca.pem"
USER=elk_user
PW=elk_pw

TODAY=`date +%Y.%m.%d`
URL="https://elk-url:9200"

close_week_old_indices()
{
  WEEK_FROM_TODAY=`date +%Y.%m.%d -d -7days`
  INDEX=logstash-*-$WEEK_FROM_TODAY
  res=`curl -XGET $URL/$INDEX --cacert $CERT -u $USER:$PW`
  if [[$res != "{}"]]; then
    # Close indices a week old
    curl -XPOST $URL/$INDEX/_close --cacert $CERT -u $USER:$PW
  fi
}

delete_90day_old_indices()
{
  DAYS90_FROM_TODAY=`date +%Y.%m.%d -d -90days`
  INDEX=logstash-*-$DAYS90_FROM_TODAY
  res=`curl -XGET $URL/$INDEX --cacert $CERT -u $USER:$PW`
  if [[$res != "{}"]]; then
    # Delete indices 90 days old
    curl -XDELETE $URL/$INDEX --cacert $CERT -u $USER:$PW
  fi
}

# main()
close_week_old_indices
delete_90day_old_indices
```

---

<div class="post-metadata">

**Author:** ![theuntergeek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/theuntergeek/32/44961_2.png) [@theuntergeek](https://discuss.elastic.co/u/theuntergeek)\
**Post date:** [April 7, 2017, 8:59pm UTC](https://discuss.elastic.co/t/data-retention/81629/5 "2017-04-07T20:59:55Z")

</div>

@ZillaG Was Curator too hard?

---

<div class="post-metadata">

**Author:** ![ZillaG](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zillag/32/10505_2.png) [@ZillaG](https://discuss.elastic.co/u/ZillaG)\
**Post date:** [April 7, 2017, 9:13pm UTC](https://discuss.elastic.co/t/data-retention/81629/6 "2017-04-07T21:13:24Z")

</div>

It's featurr-rich and I didn't need all those features.

---

<div class="post-metadata">

**Author:** ![saad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/saad/32/17214_2.png) [@saad](https://discuss.elastic.co/u/saad)\
**Post date:** [April 11, 2017, 1:59am UTC](https://discuss.elastic.co/t/data-retention/81629/7 "2017-04-11T01:59:56Z")

</div>

Thank You

---

<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:** [May 9, 2017, 2:03am UTC](https://discuss.elastic.co/t/data-retention/81629/8 "2017-05-09T02:03:37Z")

</div>

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