# Delete vs deletebyquery

**URL:** https://discuss.elastic.co/t/delete-vs-deletebyquery/308183
**Category:** Elasticsearch
**Created:** [June 26, 2022, 12:31pm UTC](https://discuss.elastic.co/t/delete-vs-deletebyquery/308183 "2022-06-26T12:31:17Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![avnere](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@avnere](https://discuss.elastic.co/u/avnere)
#### Post date: [June 26, 2022, 12:31pm UTC](https://discuss.elastic.co/t/delete-vs-deletebyquery/308183/1 "2022-06-26T12:31:17Z")

</div>

Hi,

I want to delete over 10 million documents.  
Which approach is better and faster? Loop and delete each document with DELETE //\_doc/\<\_id\> OR perform deletebyquery?

Thanks...

---

<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: [June 26, 2022, 1:10pm UTC](https://discuss.elastic.co/t/delete-vs-deletebyquery/308183/2 "2022-06-26T13:10:21Z")

</div>

Are they all part of the same index?  
How many documents do you have in total?

---

<div class="post-metadata">

### Author: ![avnere](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@avnere](https://discuss.elastic.co/u/avnere)
#### Post date: [June 26, 2022, 2:04pm UTC](https://discuss.elastic.co/t/delete-vs-deletebyquery/308183/3 "2022-06-26T14:04:09Z")

</div>

Index size is 50GB.  
They can be in 1-2 indexes.

---

<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: [June 26, 2022, 2:56pm UTC](https://discuss.elastic.co/t/delete-vs-deletebyquery/308183/4 "2022-06-26T14:56:20Z")

</div>

I did not mean the size on disk but the number of documents.

What is the output of:

```auto
GET /_cat/indices?v

```

---

<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: [June 26, 2022, 3:00pm UTC](https://discuss.elastic.co/t/delete-vs-deletebyquery/308183/5 "2022-06-26T15:00:17Z")

</div>

I do not think there is much difference. Behind the scenes delete by query runs a query to get the ids and then take these and send bulk delete requests by id. This is probably what you would be doing anyway. It will reduce network round trips but maybe use lower concurrency, so it could go either way.

---

<div class="post-metadata">

### Author: ![avnere](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@avnere](https://discuss.elastic.co/u/avnere)
#### Post date: [June 26, 2022, 3:22pm UTC](https://discuss.elastic.co/t/delete-vs-deletebyquery/308183/6 "2022-06-26T15:22:31Z")

</div>

> [@dadoonet](#):
>
> but the number of documents.

```auto
green open base_elements_rwr-2022.06.23-000020 JpzGSsMzR5-zAMdAi3Qp9w 1 0 73474700 0 16gb 16gb

```

---

<div class="post-metadata">

### Author: ![avnere](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@avnere](https://discuss.elastic.co/u/avnere)
#### Post date: [June 27, 2022, 11:24am UTC](https://discuss.elastic.co/t/delete-vs-deletebyquery/308183/7 "2022-06-27T11:24:45Z")

</div>

1. Is the delete impact write performance?

2. What is the recommended approach to delete documents (over 10 million) without impacting write\read performance?

3. Do I have to reindex after each delete?

4. If I run delete with wait\_for\_completion=false, should I delete the task manually after completed? If yes how (It is system index)?

---

<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: [June 27, 2022, 11:54am UTC](https://discuss.elastic.co/t/delete-vs-deletebyquery/308183/8 "2022-06-27T11:54:59Z")

</div>

> [@avnere](#):
>
> Is the delete impact write performance?

Deletes in Elasticsearch are basically a soft delete where a tombstone record is created and the original data removed after merging. It will therefore almost act as an update and will result in disk I/O and additional merging activity which can affect read and write performence.

> [@avnere](#):
>
> What is the recommended approach to delete documents (over 10 million) without impacting write\read performance?

The most efficient way to delete data from Elasticsearch is to delete complete indices. This is why time-based indices are often used for immutable data like logs. If you are deleteing data based on age this may be an option. If you have more complex criteria there is no way to delete without potentially affecting performance in some way. You can do it slowly and spread out the load but that is it.

> [@avnere](#):
>
> Do I have to reindex after each delete?

No, but you may want to run a refresh if you are have modified the refresh interval.

> [@avnere](#):
>
> If I run delete with wait\_for\_completion=false, should I delete the task manually after completed? If yes how (It is system index)?

No.

---

<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 25, 2022, 11:55am UTC](https://discuss.elastic.co/t/delete-vs-deletebyquery/308183/9 "2022-07-25T11:55:00Z")

</div>

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