# Deleting a parent document with no Child Documents

**URL:** <https://discuss.elastic.co/t/deleting-a-parent-document-with-no-child-documents/17690>\
**Category:** Elasticsearch\
**Created:** [May 23, 2014, 11:03am UTC](https://discuss.elastic.co/t/deleting-a-parent-document-with-no-child-documents/17690 "2014-05-23T11:03:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ishan\_Durugkar](https://avatars.discourse-cdn.com/v4/letter/i/ed655f/32.png) [@Ishan\_Durugkar](https://discuss.elastic.co/u/Ishan_Durugkar)\
**Post date:** [May 23, 2014, 11:03am UTC](https://discuss.elastic.co/t/deleting-a-parent-document-with-no-child-documents/17690/1 "2014-05-23T11:03:46Z")

</div>

Hi,

I am new to elasticsearch. I was trying to create a parent-child mapping  
where the children would be frequently added and removed.  
Every Parent should have at least one child.

What I want to do is, if a child is deleted, I want to check if the parent  
has other children documents, and if not, delete that parent document.  
How do I do this?

I was trying a delete by query, but I am not sure which query I should send.

Thanks for the help! 🙂

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/282aca7b-4b38-41ab-91c5-42094083d8d9%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/282aca7b-4b38-41ab-91c5-42094083d8d9%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Hannes\_Korte](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hannes_korte/32/909_2.png) [@Hannes\_Korte](https://discuss.elastic.co/u/Hannes_Korte)\
**Post date:** [May 24, 2014, 11:22am UTC](https://discuss.elastic.co/t/deleting-a-parent-document-with-no-child-documents/17690/2 "2014-05-24T11:22:38Z")

</div>

Hi Ishan,

I usually do that using a query like this:

{  
"query" : {  
"constant\_score" : {  
"filter" : {  
"not" : {  
"filter" : {  
"has\_parent" : {  
"query" : {  
"match\_all" : { }  
},  
"parent\_type" : "### PARENT TYPE ###"  
}  
}  
}  
}  
}  
}  
}

Good luck! Better test the query before deleting! 😉

Best regards,  
Hannes

On 23.05.2014 13:03, Ishan Durugkar wrote:

> Hi,
> 
> I am new to elasticsearch. I was trying to create a parent-child mapping  
> where the children would be frequently added and removed.  
> Every Parent should have at least one child.
> 
> What I want to do is, if a child is deleted, I want to check if the parent  
> has other children documents, and if not, delete that parent document.  
> How do I do this?
> 
> I was trying a delete by query, but I am not sure which query I should send.
> 
> Thanks for the help! 🙂

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/538080FE.70603%40hkorte.com](https://groups.google.com/d/msgid/elasticsearch/538080FE.70603%40hkorte.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Ishan\_Durugkar](https://avatars.discourse-cdn.com/v4/letter/i/ed655f/32.png) [@Ishan\_Durugkar](https://discuss.elastic.co/u/Ishan_Durugkar)\
**Post date:** [May 26, 2014, 6:02am UTC](https://discuss.elastic.co/t/deleting-a-parent-document-with-no-child-documents/17690/3 "2014-05-26T06:02:12Z")

</div>

Hi Hannes,

I actually wanted to find parent with no child, but I modified your query  
accordingly and it is working!

Thanks! 🙂  
Ishan

On Saturday, May 24, 2014 4:52:56 PM UTC+5:30, Hannes Korte wrote:

> Hi Ishan,
> 
> I usually do that using a query like this:
> 
> {  
> "query" : {  
> "constant\_score" : {  
> "filter" : {  
> "not" : {  
> "filter" : {  
> "has\_parent" : {  
> "query" : {  
> "match\_all" : { }  
> },  
> "parent\_type" : "### PARENT TYPE ###"  
> }  
> }  
> }  
> }  
> }  
> }  
> }
> 
> Good luck! Better test the query before deleting! 😉
> 
> Best regards,  
> Hannes
> 
> On 23.05.2014 13:03, Ishan Durugkar wrote:
> 
> > Hi,
> > 
> > I am new to elasticsearch. I was trying to create a parent-child mapping  
> > where the children would be frequently added and removed.  
> > Every Parent should have at least one child.
> > 
> > What I want to do is, if a child is deleted, I want to check if the  
> > parent  
> > has other children documents, and if not, delete that parent document.  
> > How do I do this?
> > 
> > I was trying a delete by query, but I am not sure which query I should  
> > send.
> > 
> > Thanks for the help! 🙂

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/e42da428-4d9e-4d64-a094-2d0d06c3077f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/e42da428-4d9e-4d64-a094-2d0d06c3077f%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 1:27am UTC](https://discuss.elastic.co/t/deleting-a-parent-document-with-no-child-documents/17690/4 "2017-07-06T01:27:01Z")

</div>


