# Elastic 5 - DeleteByQuery

**URL:** <https://discuss.elastic.co/t/elastic-5-deletebyquery/65721>\
**Category:** Elasticsearch\
**Created:** [November 10, 2016, 8:38pm UTC](https://discuss.elastic.co/t/elastic-5-deletebyquery/65721 "2016-11-10T20:38:13Z")\
**Posts on this page:** 6\
**Page:** 1

<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 10, 2016, 8:38pm UTC](https://discuss.elastic.co/t/elastic-5-deletebyquery/65721/1 "2016-11-10T20:38:13Z")

</div>

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(), DeleteByQueryAction.INSTANCE)  
.setIndices(index)  
.setTypes(type)  
.setRouting( **parentId** )  
.setQuery(aQuery)  
.execute()  
.actionGet();

return resp.getTotalDeleted();

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [November 10, 2016, 9:06pm UTC](https://discuss.elastic.co/t/elastic-5-deletebyquery/65721/2 "2016-11-10T21:06:55Z")

</div>

Delete by query has moved into the `reindex` module so it can share code with the reindex feature added in 2.3. It shares the same superclass for its request objects as reindex and update-by-query.

I think it looks something like:

```java
DeleteByQueryRequestBuilder req = new DeleteByQueryRequestBuilder(this.getElasticClient(), DeleteByQueryAction.INSTANCE);
req.source()
  .setIndices(index)
  .setTypes(type)
  .setRouting(parentId)
  .setQuery(aQuery);
DeleteByQueryResponse resp = req.get();

```

---

<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 11, 2016, 12:35pm UTC](https://discuss.elastic.co/t/elastic-5-deletebyquery/65721/3 "2016-11-11T12:35:37Z")

</div>

Thanks a lot. My tests were around your solution except that I didn't find that magical **.source()** to apply everything to it.

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [November 11, 2016, 2:24pm UTC](https://discuss.elastic.co/t/elastic-5-deletebyquery/65721/4 "2016-11-11T14:24:02Z")

</div>

Sorry! I wrote the API for reindex first and everything else kind of flowed from it. `.source()` in this case means "the source of the documents to index". Then we ported delete-by-query so it could take advantage of throttling and the rest of the infrastructure coming in to reindex. In 5.1 it'll be parallelizable by setting `slices`, for example. But that means the API changed totally. Sorry!

---

<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 11, 2016, 6:05pm UTC](https://discuss.elastic.co/t/elastic-5-deletebyquery/65721/5 "2016-11-11T18:05:21Z")

</div>

No need to be sorry, I got my answer quicker than the time I spent to look for it myself. That was an easier update than going from 1.x to 2.x.

---

<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 9, 2016, 6:05pm UTC](https://discuss.elastic.co/t/elastic-5-deletebyquery/65721/6 "2016-12-09T18:05:51Z")

</div>

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