# Delete child documents without parent using Java API

**URL:** <https://discuss.elastic.co/t/delete-child-documents-without-parent-using-java-api/246276>\
**Category:** Elasticsearch\
**Created:** [August 25, 2020, 11:59am UTC](https://discuss.elastic.co/t/delete-child-documents-without-parent-using-java-api/246276 "2020-08-25T11:59:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![makons](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/makons/32/74314_2.png) [@makons](https://discuss.elastic.co/u/makons)\
**Post date:** [August 25, 2020, 11:59am UTC](https://discuss.elastic.co/t/delete-child-documents-without-parent-using-java-api/246276/1 "2020-08-25T11:59:24Z")

</div>

As the title says, I'm trying to delete all the parentless child documents using ES Java API. If I got things correctly, I need to use DeleteByQuery, and my proposed solution is this:

```auto
val allParentlessChildren = QueryBuilders
    .boolQuery()
    .mustNot(JoinQueryBuilders.hasParentQuery(
      "my_parent",
      QueryBuilders.matchAllQuery(),
      false)
    )
val delete = new DeleteByQuery.Builder(allParentlessChildren.toString)
    .addIndex("my_index")
    .addType("my_child")
    .build()

```

However, I get `routing_missing_exception`. [Investigating online](https://www.elastic.co/guide/en/elasticsearch/reference/5.5/docs-delete.html#delete-parent), it seems I need to set parent type for routing, however, apart from specifying it in `hasParentQuery` idk where else I need to add it?

Although I found [some examples](https://discuss.elastic.co/t/deleting-a-parent-document-with-no-child-documents/17690) how to do it with REST API, I wasn't able to find ones that use Java API, so hopefully someone can help out.

I'm using Elasticsearch 5.5.

I've posted it on [Stackoverflow](https://stackoverflow.com/questions/63575169/elasticsearch-java-api-delete-child-documents-without-parent) as well.

---

<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 22, 2020, 11:59am UTC](https://discuss.elastic.co/t/delete-child-documents-without-parent-using-java-api/246276/2 "2020-09-22T11:59:39Z")

</div>

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