# Is Delete by query a clean operation?

**URL:** https://discuss.elastic.co/t/is-delete-by-query-a-clean-operation/295778
**Category:** Elasticsearch
**Created:** [January 30, 2022, 1:03pm UTC](https://discuss.elastic.co/t/is-delete-by-query-a-clean-operation/295778 "2022-01-30T13:03:43Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![yash\_196](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yash_196/32/99273_2.png) [@yash\_196](https://discuss.elastic.co/u/yash_196)
#### Post date: [January 30, 2022, 1:03pm UTC](https://discuss.elastic.co/t/is-delete-by-query-a-clean-operation/295778/1 "2022-01-30T13:03:43Z")

</div>

Hi Everyone,

I wanted to know if delete by query is a clean API? Are the resources (Memory or I/O) released gracefully after the query is complete or does it still hold some resource which might affect perormance?

---

<div class="post-metadata">

### Author: ![DavidTurner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidturner/32/22453_2.png) [@DavidTurner](https://discuss.elastic.co/u/DavidTurner)
#### Post date: [January 30, 2022, 1:15pm UTC](https://discuss.elastic.co/t/is-delete-by-query-a-clean-operation/295778/2 "2022-01-30T13:15:59Z")

</div>

All resources are released when no longer needed (at least, if they aren't then that's a bug).

---

<div class="post-metadata">

### Author: ![yash\_196](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yash_196/32/99273_2.png) [@yash\_196](https://discuss.elastic.co/u/yash_196)
#### Post date: [January 30, 2022, 6:37pm UTC](https://discuss.elastic.co/t/is-delete-by-query-a-clean-operation/295778/3 "2022-01-30T18:37:33Z")

</div>

Thanks for the reply @DavidTurner. I had one more question related to delete by query: How much time does it take to execute, let's say on some given size of data?

---

<div class="post-metadata">

### Author: ![DavidTurner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidturner/32/22453_2.png) [@DavidTurner](https://discuss.elastic.co/u/DavidTurner)
#### Post date: [January 30, 2022, 6:48pm UTC](https://discuss.elastic.co/t/is-delete-by-query-a-clean-operation/295778/4 "2022-01-30T18:48:47Z")

</div>

That depends on so many details of the data and the cluster configuration that it's basically impossible to answer. You should benchmark it on a representative sample of your data.

---

<div class="post-metadata">

### Author: ![yash\_196](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yash_196/32/99273_2.png) [@yash\_196](https://discuss.elastic.co/u/yash_196)
#### Post date: [January 31, 2022, 5:30am UTC](https://discuss.elastic.co/t/is-delete-by-query-a-clean-operation/295778/5 "2022-01-31T05:30:56Z")

</div>

@DavidTurner do you have any sample use case based on your experience? I can use that for a guesstimate.

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [January 31, 2022, 6:25am UTC](https://discuss.elastic.co/t/is-delete-by-query-a-clean-operation/295778/6 "2022-01-31T06:25:16Z")

</div>

Elasticsearch relies on Lucene, which used immutable segments to store data. Deleting a document therefore does not happen in place, and a delete is therefore similar to an update with respect to performance as a tombstone record need to be written to a new segment replacing the deleted document. The actual document is then removed during merging of segments at some later time. Deleting lots of documents can therefore be very resource intensive and take a while to run.

How much data do you have? How much do you delete each time? Are you planning on running multiple deletes in parallel? How frequently are you instering or updating data apart from the deletions?

---

<div class="post-metadata">

### Author: ![yash\_196](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yash_196/32/99273_2.png) [@yash\_196](https://discuss.elastic.co/u/yash_196)
#### Post date: [January 31, 2022, 12:41pm UTC](https://discuss.elastic.co/t/is-delete-by-query-a-clean-operation/295778/7 "2022-01-31T12:41:36Z")

</div>

Hi @Christian_Dahlqvist to answer your questions:

How much data do you have?  
Ans - 50 GB in 1 Index (1 Shard per index)

How much do you delete each time?  
Ans - 10% ~ 5 GB of Data

Are you planning on running multiple deletes in parallel?  
Ans - Yes, around 1000 parallel delete operations will be running to delete 5 GB of data.

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [January 31, 2022, 12:53pm UTC](https://discuss.elastic.co/t/is-delete-by-query-a-clean-operation/295778/8 "2022-01-31T12:53:05Z")

</div>

> [@yash\_196](#):
>
> Ans - Yes, around 1000 parallel delete operations will be running to delete 5 GB of data.

I suspect that will be a problem and would recommend you test/benchmark to make sure.

---

<div class="post-metadata">

### Author: ![yash\_196](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yash_196/32/99273_2.png) [@yash\_196](https://discuss.elastic.co/u/yash_196)
#### Post date: [February 1, 2022, 5:42am UTC](https://discuss.elastic.co/t/is-delete-by-query-a-clean-operation/295778/9 "2022-02-01T05:42:56Z")

</div>

@Christian_Dahlqvist How much time will it take approximately if we trigger 1 query which deletes 5 GB of data from an index of 50 GB?

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [February 1, 2022, 7:02am UTC](https://discuss.elastic.co/t/is-delete-by-query-a-clean-operation/295778/10 "2022-02-01T07:02:00Z")

</div>

It depends on your cluster and data as well as what other load the cluster is under and the query so you need to test. If you are looking for an order of magnitude it may be similar to how long it would take to update the same amount of data. Deleting a lot of data can take a long time this way.

---

<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: [March 1, 2022, 7:02am UTC](https://discuss.elastic.co/t/is-delete-by-query-a-clean-operation/295778/11 "2022-03-01T07:02:14Z")

</div>

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