# Purge last N documents from ElasticSerach

**URL:** https://discuss.elastic.co/t/purge-last-n-documents-from-elasticserach/189229
**Category:** Elasticsearch
**Created:** [July 7, 2019, 2:06am UTC](https://discuss.elastic.co/t/purge-last-n-documents-from-elasticserach/189229 "2019-07-07T02:06:17Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Dileep\_Mandapam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dileep_mandapam/32/48742_2.png) [@Dileep\_Mandapam](https://discuss.elastic.co/u/Dileep_Mandapam)
#### Post date: [July 7, 2019, 2:06am UTC](https://discuss.elastic.co/t/purge-last-n-documents-from-elasticserach/189229/1 "2019-07-07T02:06:17Z")

</div>

Is there a way to purge last N documents from Elasticsearch?.  
Use-case is, Purging documents based on the count.  
For example, assume that the document count threshold was set to 1million and current document count is 1.1 million. I need to purge the last 0.1 million documents.

---

<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: [July 7, 2019, 2:37am UTC](https://discuss.elastic.co/t/purge-last-n-documents-from-elasticserach/189229/2 "2019-07-07T02:37:49Z")

</div>

Welcome.

Not really. Elasticsearch does not really know what are the first or the last documents unless you have a field in the documentation which can tell that (a counter, a date...).

Did you mean to purge the last N documents or the first N documents? What exactly is the use case for this?

---

<div class="post-metadata">

### Author: ![Dileep\_Mandapam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dileep_mandapam/32/48742_2.png) [@Dileep\_Mandapam](https://discuss.elastic.co/u/Dileep_Mandapam)
#### Post date: [July 7, 2019, 1:00pm UTC](https://discuss.elastic.co/t/purge-last-n-documents-from-elasticserach/189229/3 "2019-07-07T13:00:57Z")

</div>

We have a timestamp field in the report.  
Use-case: Purge criteria is based on number of documents.  
Assume that doucment count threshold is 1 million. If total count of documents is 1.2 million then need to delete last 0.2 million documents.

---

<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: [July 7, 2019, 1:51pm UTC](https://discuss.elastic.co/t/purge-last-n-documents-from-elasticserach/189229/4 "2019-07-07T13:51:37Z")

</div>

I don't see a way to do that.

And I don't understand the need you described. Why do you need such a threshold?

---

<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: [August 4, 2019, 1:51pm UTC](https://discuss.elastic.co/t/purge-last-n-documents-from-elasticserach/189229/5 "2019-08-04T13:51:45Z")

</div>

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