# When deleting by query api all my documents get deleted

**URL:** https://discuss.elastic.co/t/when-deleting-by-query-api-all-my-documents-get-deleted/115341
**Category:** Elasticsearch
**Created:** [January 12, 2018, 5:08pm UTC](https://discuss.elastic.co/t/when-deleting-by-query-api-all-my-documents-get-deleted/115341 "2018-01-12T17:08:06Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Martin\_Rioux](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/martin_rioux/32/26528_2.png) [@Martin\_Rioux](https://discuss.elastic.co/u/Martin_Rioux)
#### Post date: [January 12, 2018, 5:08pm UTC](https://discuss.elastic.co/t/when-deleting-by-query-api-all-my-documents-get-deleted/115341/1 "2018-01-12T17:08:06Z")

</div>

Hi, I performed the same operation for 6 months to delete document older than 6 months with the delete by query api.

"query": {  
"range": {  
"created": {  
"lte": "now-150d/d"  
}  
}  
}

I don't know why but today all my documents get deleted with this request. I have a staging server with the same data and everything is OK. I don\<t know if there is a safer way to delete data. I run that request every 10 minutes and it delete about 200 documents each time. But today this request delete over 1Million 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: [January 12, 2018, 6:09pm UTC](https://discuss.elastic.co/t/when-deleting-by-query-api-all-my-documents-get-deleted/115341/2 "2018-01-12T18:09:07Z")

</div>

It often happens in such case that the user sent a bad request where the body is totally ignored so the query is "match\_all"...

Can you see in your history exactly what you did?

Best thing to do is to use time based indices. It's much more efficient IMO.

---

<div class="post-metadata">

### Author: ![Martin\_Rioux](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/martin_rioux/32/26528_2.png) [@Martin\_Rioux](https://discuss.elastic.co/u/Martin_Rioux)
#### Post date: [January 12, 2018, 6:25pm UTC](https://discuss.elastic.co/t/when-deleting-by-query-api-all-my-documents-get-deleted/115341/3 "2018-01-12T18:25:48Z")

</div>

> [@dadoonet](#):
>
> IMO

Thanks for your answer. This request is called in a cronjob and never done manually. I can't use time base indices because some documents will live forever in ES. (new developments ). I just one to find a more secure way to delete documents.

---

<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: [February 9, 2018, 6:26pm UTC](https://discuss.elastic.co/t/when-deleting-by-query-api-all-my-documents-get-deleted/115341/4 "2018-02-09T18:26:57Z")

</div>

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