# Delete old documents

**URL:** https://discuss.elastic.co/t/delete-old-documents/254591
**Category:** Elasticsearch
**Created:** [November 7, 2020, 12:50am UTC](https://discuss.elastic.co/t/delete-old-documents/254591 "2020-11-07T00:50:56Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![smruthip](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@smruthip](https://discuss.elastic.co/u/smruthip)
#### Post date: [November 7, 2020, 12:50am UTC](https://discuss.elastic.co/t/delete-old-documents/254591/1 "2020-11-07T00:50:56Z")

</div>

Is there a way to delete old documents based on a filed which has timestamp?  
What I want to do is just keep the latest document per id

example  
id. timestamp  
1 1pm  
1 2pm  
1 3pm  
1 4pm  
2. 3pm  
2. 4pm  
2. 6pm  
2. 7pm

I want to keep only the 4 pm document for id 1 and delete all the others, similarly for id 2, want to keep only 7pm document and delete all the rest.

Thank you!

---

<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: [November 7, 2020, 3:00am UTC](https://discuss.elastic.co/t/delete-old-documents/254591/2 "2020-11-07T03:00:22Z")

</div>

If you stored the hour of the day as a field in your document and have the id field as well, you can create a query to select those documents and make sure it works well using the search API.

Then use that query in a Delete By Query API call.

---

<div class="post-metadata">

### Author: ![smruthip](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@smruthip](https://discuss.elastic.co/u/smruthip)
#### Post date: [November 7, 2020, 6:15am UTC](https://discuss.elastic.co/t/delete-old-documents/254591/3 "2020-11-07T06:15:53Z")

</div>

Thank you David. So I have a query to give me the most recent document for each id. Now I want to delete all the other documents for that id except the latest document. I am really new to Elastic search and not sure how the query will look like. Please could you guide me in the right direction?

{  
"aggs":{  
"top\_tags":{  
"terms":{  
"field":"id.keyword"  
},  
"aggs":{  
"top\_sales\_hits":{  
"top\_hits":{  
"sort":[  
{  
"Time":{  
"order":"desc"  
}  
}  
]  
}

---

<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: [November 10, 2020, 11:23am UTC](https://discuss.elastic.co/t/delete-old-documents/254591/4 "2020-11-10T11:23:46Z")

</div>

I believe you need to write some kind of a manual script to do that. Like getting all `_id` that needs to be deleted and call for each the DELETE document API?

But why you are in that situation? Why not using the `id` field of your document as the `_id` of the elasticsearch document?

In that case, anytime you are writing a new "version" of the document, a more recent version I mean, old version will be overwritten.  
That way, there is nothing else to do.

---

<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: [December 8, 2020, 11:23am UTC](https://discuss.elastic.co/t/delete-old-documents/254591/5 "2020-12-08T11:23:52Z")

</div>

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