# Delete by Query in ES 5.0 Java Client

**URL:** <https://discuss.elastic.co/t/delete-by-query-in-es-5-0-java-client/66948>\
**Category:** Elasticsearch\
**Created:** [November 23, 2016, 7:13am UTC](https://discuss.elastic.co/t/delete-by-query-in-es-5-0-java-client/66948 "2016-11-23T07:13:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiouser](https://avatars.discourse-cdn.com/v4/letter/j/e99b99/32.png) [@jiouser](https://discuss.elastic.co/u/jiouser)\
**Post date:** [November 23, 2016, 7:13am UTC](https://discuss.elastic.co/t/delete-by-query-in-es-5-0-java-client/66948/1 "2016-11-23T07:13:20Z")

</div>

Hello,  
Just starting off with ES. Ran into problem when trying to delete the indexed documents. Seems like the Delete by Query is no longer available in ES 5.0 Java client.  
How do I achieve selective delete of documents from the Index using Java client. i.e. (A query determines which all documents to be deleted).  
I am running ES in embedded(local) mode for the time being. Later it will run in production mode(Client/Server).

Can anyone please help me out.

---

<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:** [November 23, 2016, 8:18am UTC](https://discuss.elastic.co/t/delete-by-query-in-es-5-0-java-client/66948/2 "2016-11-23T08:18:25Z")

</div>

Is it the same question as the following?

> [@Elastic 5 - DeleteByQuery](https://discuss.elastic.co/t/elastic-5-deletebyquery/65721):
>
> Hi I'm upgrading our Java elasticsearch layer from 2.3.3 to 5.0. Everything seems fine excepts the DeleteByQuery. Our current code is pretty simple, we delete all documents that are having a specific parent. But sadly, I'm completely unabled to reproduce that using 5.0 DeleteByQueryRequestBuilder. Any help would be appreciated. QueryBuilder aQuery = QueryBuilders.termQuery("\_parent", parentId); DeleteByQueryResponse resp = new DeleteByQueryRequestBuilder(this.getElasticClient(), DeleteByQ…

---

<div class="post-metadata">

**Author:** ![cthibodeau](https://avatars.discourse-cdn.com/v4/letter/c/b487fb/32.png) [@cthibodeau](https://discuss.elastic.co/u/cthibodeau)\
**Post date:** [November 23, 2016, 12:46pm UTC](https://discuss.elastic.co/t/delete-by-query-in-es-5-0-java-client/66948/3 "2016-11-23T12:46:56Z")

</div>

Hi. It looks the same.

Here is my code, but I haven't tried it yet. But there is no compilation error.

```
        QueryBuilder aQuery = QueryBuilders.termQuery("_parent", idParent);    

        DeleteByQueryRequestBuilder req = new DeleteByQueryRequestBuilder(this.getElasticClient(), DeleteByQueryAction.INSTANCE);
        **req.source()** // here is the trick
          .setIndices(index)
          .setTypes(type)
          .setRouting(idParent)
          .setQuery(aQuery);
        
        BulkIndexByScrollResponse resp = req.get();

        return resp.getDeleted();
```

---

<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:** [December 21, 2016, 12:47pm UTC](https://discuss.elastic.co/t/delete-by-query-in-es-5-0-java-client/66948/4 "2016-12-21T12:47:25Z")

</div>

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