# How do i delete child docs in my parent-child relationship automatically in elasticsearch after a particular interval is finished

**URL:** <https://discuss.elastic.co/t/how-do-i-delete-child-docs-in-my-parent-child-relationship-automatically-in-elasticsearch-after-a-particular-interval-is-finished/197645>\
**Category:** Elasticsearch\
**Created:** [September 2, 2019, 5:27am UTC](https://discuss.elastic.co/t/how-do-i-delete-child-docs-in-my-parent-child-relationship-automatically-in-elasticsearch-after-a-particular-interval-is-finished/197645 "2019-09-02T05:27:03Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![yash.tandon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yash.tandon/32/53428_2.png) [@yash.tandon](https://discuss.elastic.co/u/yash.tandon)\
**Post date:** [September 2, 2019, 5:27am UTC](https://discuss.elastic.co/t/how-do-i-delete-child-docs-in-my-parent-child-relationship-automatically-in-elasticsearch-after-a-particular-interval-is-finished/197645/1 "2019-09-02T05:27:03Z")

</div>

link: [https://stackoverflow.com/q/57704269/11432290](https://stackoverflow.com/q/57704269/11432290)

Can someone help me with this as i wanted to delete my child docs in my parent child relationship as described above in the link. Your guidance will be highly appreciated.

---

<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:** [September 2, 2019, 5:41am UTC](https://discuss.elastic.co/t/how-do-i-delete-child-docs-in-my-parent-child-relationship-automatically-in-elasticsearch-after-a-particular-interval-is-finished/197645/2 "2019-09-02T05:41:22Z")

</div>

Both Curator and ILM rely on deleting complete indices, so will not suit your use case as you will need to delete specific document from the index/indices. To do this you will need to use the [delete-by-query API](https://www.elastic.co/guide/en/elasticsearch/reference/7.3/docs-delete-by-query.html). You will however need to schedule this yourself as I do not think there is any suitable way to do that in Elasticsearch.

---

<div class="post-metadata">

**Author:** ![yash.tandon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yash.tandon/32/53428_2.png) [@yash.tandon](https://discuss.elastic.co/u/yash.tandon)\
**Post date:** [September 2, 2019, 5:50am UTC](https://discuss.elastic.co/t/how-do-i-delete-child-docs-in-my-parent-child-relationship-automatically-in-elasticsearch-after-a-particular-interval-is-finished/197645/3 "2019-09-02T05:50:47Z")

</div>

But i think that delete\_by\_query is a very expensive operation so will it be feasible to run it for millions of documents daily.

---

<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:** [September 2, 2019, 5:52am UTC](https://discuss.elastic.co/t/how-do-i-delete-child-docs-in-my-parent-child-relationship-automatically-in-elasticsearch-after-a-particular-interval-is-finished/197645/4 "2019-09-02T05:52:52Z")

</div>

It is very expensive as each delete is an update behind the scenes. It is however the only way to delete select documents from an index, so I do not think you have much choice unless you can change your data model to flat documents in time-based indices, which are much more efficient to delete.

---

<div class="post-metadata">

**Author:** ![yash.tandon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yash.tandon/32/53428_2.png) [@yash.tandon](https://discuss.elastic.co/u/yash.tandon)\
**Post date:** [September 2, 2019, 5:55am UTC](https://discuss.elastic.co/t/how-do-i-delete-child-docs-in-my-parent-child-relationship-automatically-in-elasticsearch-after-a-particular-interval-is-finished/197645/5 "2019-09-02T05:55:10Z")

</div>

well i guess then, thier must be a way in parent-child relationship to delete all the child docs except for parent docs but I can't seem to find one.

---

<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:** [September 2, 2019, 5:57am UTC](https://discuss.elastic.co/t/how-do-i-delete-child-docs-in-my-parent-child-relationship-automatically-in-elasticsearch-after-a-particular-interval-is-finished/197645/6 "2019-09-02T05:57:50Z")

</div>

As far as I know there is not, and even if it were it would still need to do it the same way delete-by-query works.

If you have time-based data, why are you using parent-child in the first place? Why not denormalize your data model?

---

<div class="post-metadata">

**Author:** ![yash.tandon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yash.tandon/32/53428_2.png) [@yash.tandon](https://discuss.elastic.co/u/yash.tandon)\
**Post date:** [September 2, 2019, 5:58am UTC](https://discuss.elastic.co/t/how-do-i-delete-child-docs-in-my-parent-child-relationship-automatically-in-elasticsearch-after-a-particular-interval-is-finished/197645/7 "2019-09-02T05:58:54Z")

</div>

thank you, for your help.

---

<div class="post-metadata">

**Author:** ![yash.tandon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yash.tandon/32/53428_2.png) [@yash.tandon](https://discuss.elastic.co/u/yash.tandon)\
**Post date:** [September 2, 2019, 6:01am UTC](https://discuss.elastic.co/t/how-do-i-delete-child-docs-in-my-parent-child-relationship-automatically-in-elasticsearch-after-a-particular-interval-is-finished/197645/8 "2019-09-02T06:01:18Z")

</div>

well the business use case is such that the parent child is according to my needs.

---

<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:** [September 2, 2019, 6:01am UTC](https://discuss.elastic.co/t/how-do-i-delete-child-docs-in-my-parent-child-relationship-automatically-in-elasticsearch-after-a-particular-interval-is-finished/197645/9 "2019-09-02T06:01:51Z")

</div>

What are the requirements that drive this? Do you have parent documents that are very large or change frequently?

---

<div class="post-metadata">

**Author:** ![yash.tandon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yash.tandon/32/53428_2.png) [@yash.tandon](https://discuss.elastic.co/u/yash.tandon)\
**Post date:** [September 2, 2019, 6:06am UTC](https://discuss.elastic.co/t/how-do-i-delete-child-docs-in-my-parent-child-relationship-automatically-in-elasticsearch-after-a-particular-interval-is-finished/197645/10 "2019-09-02T06:06:09Z")

</div>

no the child docs are large and a parent can have 100's of child documents.

---

<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:** [September 2, 2019, 6:07am UTC](https://discuss.elastic.co/t/how-do-i-delete-child-docs-in-my-parent-child-relationship-automatically-in-elasticsearch-after-a-particular-interval-is-finished/197645/11 "2019-09-02T06:07:52Z")

</div>

What is driving the use of parent-child relationship then? Query requirements?

---

<div class="post-metadata">

**Author:** ![yash.tandon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yash.tandon/32/53428_2.png) [@yash.tandon](https://discuss.elastic.co/u/yash.tandon)\
**Post date:** [September 2, 2019, 6:10am UTC](https://discuss.elastic.co/t/how-do-i-delete-child-docs-in-my-parent-child-relationship-automatically-in-elasticsearch-after-a-particular-interval-is-finished/197645/12 "2019-09-02T06:10:11Z")

</div>

yes it plays a big role as with a single query i am able to segment my users. Also i have to frequently update and insert docs so re-indexing is efficient here.

---

<div class="post-metadata">

**Author:** ![yash.tandon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yash.tandon/32/53428_2.png) [@yash.tandon](https://discuss.elastic.co/u/yash.tandon)\
**Post date:** [September 2, 2019, 7:04am UTC](https://discuss.elastic.co/t/how-do-i-delete-child-docs-in-my-parent-child-relationship-automatically-in-elasticsearch-after-a-particular-interval-is-finished/197645/13 "2019-09-02T07:04:56Z")

</div>

waiting for your reply sir....

---

<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:** [September 30, 2019, 7:05am UTC](https://discuss.elastic.co/t/how-do-i-delete-child-docs-in-my-parent-child-relationship-automatically-in-elasticsearch-after-a-particular-interval-is-finished/197645/14 "2019-09-30T07:05:01Z")

</div>

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