# Deleting old versions

**URL:** <https://discuss.elastic.co/t/deleting-old-versions/4689>\
**Category:** Elasticsearch\
**Created:** [June 23, 2011, 8:45am UTC](https://discuss.elastic.co/t/deleting-old-versions/4689 "2011-06-23T08:45:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hari\_Shankar](https://avatars.discourse-cdn.com/v4/letter/h/ad7895/32.png) [@Hari\_Shankar](https://discuss.elastic.co/u/Hari_Shankar)\
**Post date:** [June 23, 2011, 8:45am UTC](https://discuss.elastic.co/t/deleting-old-versions/4689/1 "2011-06-23T08:45:10Z")

</div>

Hi,

How do you delete older versions of documents? Suppose I always need only  
the latest version, I wouldn't want to waste space storing the earlier  
versions as well.

Hari

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [June 23, 2011, 9:03am UTC](https://discuss.elastic.co/t/deleting-old-versions/4689/2 "2011-06-23T09:03:02Z")

</div>

Hi Hari

> How do you delete older versions of documents? Suppose I always need  
> only the latest version, I wouldn't want to waste space storing the  
> earlier versions as well.

One of three scenarios here:

1. You are using the same ID for each version of the doc

In this case, each time you index a doc with the same ID, the old  
version will be marked as deleted, and will no longer be visible.

These 'deleted' docs will be automatically removed at some point in the  
future when the "segments" in your index are merged. This happens in  
the background, but can be manually triggered using the optimize API.

1. You are using different IDs for each version of the doc, or no ID, in  
which case ES generates one for you.

In this case, you are out of luck. ES can only identify that one docs  
is a different version of another doc by looking at the ID.

Instead, you would have to have some way of identifying which docs are  
current, and which are old, and you can use the delete\_by\_query API to  
mark the old ones as deleted.

1. You have a rolling window

For instance, you're indexing log statements, and you want to have the  
last week's data available to you, but automatically clear out anything  
older.2

Easiest thing here would be to create a new index every day, eg  
"logs\_2011-06-23" and only insert into today's index.

For querying, you could create an alias eg "logs\_week" which points to  
"logs\_2011-06-23", "logs\_2011-06-22", etc

Then each day you would create a new index, update the alias, and delete  
the index for $today-8 days

clint

---

<div class="post-metadata">

**Author:** ![Hari\_Shankar](https://avatars.discourse-cdn.com/v4/letter/h/ad7895/32.png) [@Hari\_Shankar](https://discuss.elastic.co/u/Hari_Shankar)\
**Post date:** [June 23, 2011, 9:08am UTC](https://discuss.elastic.co/t/deleting-old-versions/4689/3 "2011-06-23T09:08:45Z")

</div>

Hi Clint,

Great.. I am basically case 1, but I saw that even though I don't specify  
any ID in the indexing request, es automatically creates a new id, e.g,  
version is increased to 2. But even after this, I saw that version 1 can be  
accessed. So you mean to say that it won't be always accessible, i.e, the  
version 1 will be visible for some time but will get removed automatically  
after some time?

Hari

On Thu, Jun 23, 2011 at 2:33 PM, Clinton Gormley [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)wrote:

> Hi Hari
> 
> > How do you delete older versions of documents? Suppose I always need  
> > only the latest version, I wouldn't want to waste space storing the  
> > earlier versions as well.
> 
> One of three scenarios here:
> 
> 1. You are using the same ID for each version of the doc
> 
> In this case, each time you index a doc with the same ID, the old  
> version will be marked as deleted, and will no longer be visible.
> 
> These 'deleted' docs will be automatically removed at some point in the  
> future when the "segments" in your index are merged. This happens in  
> the background, but can be manually triggered using the optimize API.
> 
> 1. You are using different IDs for each version of the doc, or no ID, in  
> which case ES generates one for you.
> 
> In this case, you are out of luck. ES can only identify that one docs  
> is a different version of another doc by looking at the ID.
> 
> Instead, you would have to have some way of identifying which docs are  
> current, and which are old, and you can use the delete\_by\_query API to  
> mark the old ones as deleted.
> 
> 1. You have a rolling window
> 
> For instance, you're indexing log statements, and you want to have the  
> last week's data available to you, but automatically clear out anything  
> older.2
> 
> Easiest thing here would be to create a new index every day, eg  
> "logs\_2011-06-23" and only insert into today's index.
> 
> For querying, you could create an alias eg "logs\_week" which points to  
> "logs\_2011-06-23", "logs\_2011-06-22", etc
> 
> Then each day you would create a new index, update the alias, and delete  
> the index for $today-8 days
> 
> clint

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [June 23, 2011, 9:19am UTC](https://discuss.elastic.co/t/deleting-old-versions/4689/4 "2011-06-23T09:19:44Z")

</div>

Hi Hari

> Great.. I am basically case 1, but I saw that even though I don't  
> specify any ID in the indexing request, es automatically creates a new  
> id, e.g, version is increased to 2. But even after this, I saw that  
> version 1 can be accessed. So you mean to say that it won't be always  
> accessible, i.e, the version 1 will be visible for some time but will  
> get removed automatically after some time?

Hold on, you DON'T specify an ID, but the \_version gets incremented?  
That doesn't make much sense:

curl -XPOST '[http://127.0.0.1:9200/foo/bar?pretty=1](http://127.0.0.1:9200/foo/bar?pretty=1)' -d '{"text" :"bar"}'

# [Thu Jun 23 11:17:30 2011] Response:

# {

# "ok" : true,

# "\_index" : "foo",

# "\_id" : "rXlA07w5TN2dlE8jjH-LRg",

# "\_type" : "bar",

# "\_version" : 1

# }

curl -XPOST '[http://127.0.0.1:9200/foo/bar?pretty=1](http://127.0.0.1:9200/foo/bar?pretty=1)' -d '{"text" :"bar"}'

# [Thu Jun 23 11:17:33 2011] Response:

# {

# "ok" : true,

# "\_index" : "foo",

# "\_id" : "pywWEDvqQoS\_Oqmf509lkQ",

# "\_type" : "bar",

# "\_version" : 1

# }

And if you DO specify the same ID, the older version should become  
hidden (although it may take up to 1 second for that to happen)

If you're seeing something different, please provide a gist to  
demonstrate

ta

clint

---

<div class="post-metadata">

**Author:** ![Hari\_Shankar](https://avatars.discourse-cdn.com/v4/letter/h/ad7895/32.png) [@Hari\_Shankar](https://discuss.elastic.co/u/Hari_Shankar)\
**Post date:** [June 23, 2011, 10:23am UTC](https://discuss.elastic.co/t/deleting-old-versions/4689/5 "2011-06-23T10:23:07Z")

</div>

Sorry, I meant even if a don't specify a version. I put something with  
id=id1, then I put something else with id1. Now when I do a GET, I get the  
latest version of the doc, which is fine. I just want to know if there is  
any way I can search/query the older version with the same id? Likewise, is  
there a way I can get back a specific version of the doc?

Hari

On Thu, Jun 23, 2011 at 2:49 PM, Clinton Gormley [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)wrote:

> Hi Hari
> 
> > Great.. I am basically case 1, but I saw that even though I don't  
> > specify any ID in the indexing request, es automatically creates a new  
> > id, e.g, version is increased to 2. But even after this, I saw that  
> > version 1 can be accessed. So you mean to say that it won't be always  
> > accessible, i.e, the version 1 will be visible for some time but will  
> > get removed automatically after some time?
> 
> Hold on, you DON'T specify an ID, but the \_version gets incremented?  
> That doesn't make much sense:
> 
> curl -XPOST '[http://127.0.0.1:9200/foo/bar?pretty=1](http://127.0.0.1:9200/foo/bar?pretty=1)' -d '{"text" :"bar"}'
> 
> # [Thu Jun 23 11:17:30 2011] Response:
> 
> # {
> 
> # "ok" : true,
> 
> # "\_index" : "foo",
> 
> # "\_id" : "rXlA07w5TN2dlE8jjH-LRg",
> 
> # "\_type" : "bar",
> 
> # "\_version" : 1
> 
> # }
> 
> curl -XPOST '[http://127.0.0.1:9200/foo/bar?pretty=1](http://127.0.0.1:9200/foo/bar?pretty=1)' -d '{"text" :"bar"}'
> 
> # [Thu Jun 23 11:17:33 2011] Response:
> 
> # {
> 
> # "ok" : true,
> 
> # "\_index" : "foo",
> 
> # "\_id" : "pywWEDvqQoS\_Oqmf509lkQ",
> 
> # "\_type" : "bar",
> 
> # "\_version" : 1
> 
> # }
> 
> And if you DO specify the same ID, the older version should become  
> hidden (although it may take up to 1 second for that to happen)
> 
> If you're seeing something different, please provide a gist to  
> demonstrate
> 
> ta
> 
> clint

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [June 23, 2011, 10:34am UTC](https://discuss.elastic.co/t/deleting-old-versions/4689/6 "2011-06-23T10:34:12Z")

</div>

On Thu, 2011-06-23 at 15:53 +0530, Hari Shankar wrote:

> Sorry, I meant even if a don't specify a version. I put something with  
> id=id1, then I put something else with id1. Now when I do a GET, I get  
> the latest version of the doc, which is fine. I just want to know if  
> there is any way I can search/query the older version with the same  
> id? Likewise, is there a way I can get back a specific version of the  
> doc?

I think the answer is: not without circumventing the API. You should  
only be able to get the latest version back.

clint

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [June 23, 2011, 12:02pm UTC](https://discuss.elastic.co/t/deleting-old-versions/4689/7 "2011-06-23T12:02:51Z")

</div>

There is no way to get previous versions of the doc unless you store several versions of the doc yourself under different ids / types.

On Thursday, June 23, 2011 at 1:34 PM, Clinton Gormley wrote:

> On Thu, 2011-06-23 at 15:53 +0530, Hari Shankar wrote:
> 
> > Sorry, I meant even if a don't specify a version. I put something with  
> > id=id1, then I put something else with id1. Now when I do a GET, I get  
> > the latest version of the doc, which is fine. I just want to know if  
> > there is any way I can search/query the older version with the same  
> > id? Likewise, is there a way I can get back a specific version of the  
> > doc?
> 
> I think the answer is: not without circumventing the API. You should  
> only be able to get the latest version back.
> 
> clint

---

<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:** [July 6, 2017, 4:02am UTC](https://discuss.elastic.co/t/deleting-old-versions/4689/8 "2017-07-06T04:02:49Z")

</div>


