# Bulk Delete elastic-search client

**URL:** <https://discuss.elastic.co/t/bulk-delete-elastic-search-client/353032>\
**Category:** Elastic Search\
**Tags:** elastic-site-search\
**Created:** [February 12, 2024, 3:07am UTC](https://discuss.elastic.co/t/bulk-delete-elastic-search-client/353032 "2024-02-12T03:07:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manoj\_Upadhyay](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/manoj_upadhyay/32/60156_2.png) [@Manoj\_Upadhyay](https://discuss.elastic.co/u/Manoj_Upadhyay)\
**Post date:** [February 12, 2024, 3:07am UTC](https://discuss.elastic.co/t/bulk-delete-elastic-search-client/353032/1 "2024-02-12T03:07:56Z")

</div>

How to use deletebyQuery elastic 8.x version ?

```auto
public long deleteESDocuments(String indexName, Map<String, Object> termQueryMap, Map<String, Object[]> rangeQueryMap) throws IOException{
        log.debug("Inside deleteESDocuments method");
        DeleteByQueryRequest request = null;
        BulkByScrollResponse bulkResponse = null;
        BoolQueryBuilder builder = null;
        request = new DeleteByQueryRequest(indexName);
        builder = QueryBuilders.boolQuery();
        for(String key : termQueryMap.keySet()) {
            builder.must(QueryBuilders.termQuery(key, termQueryMap.get(key)));
        }
        for (String key: rangeQueryMap.keySet()){
            Object[] range = rangeQueryMap.get(key);
            log.debug("{} range from {} to {}, ", key,range[0],range[1]);
            builder.must(QueryBuilders.rangeQuery(key).gte(range[0]).lte(range[1]));
        }
        log.debug("builderQuery is: {} ", builder);
        request.setQuery(builder);
        bulkResponse = client.deleteByQuery(request, RequestOptions.DEFAULT);
        log.debug("ES Document Deletion response: {}",bulkResponse.getStatus());
        log.debug("{} ES Document/s is/are deleted.",bulkResponse.getDeleted());

        return bulkResponse.getDeleted();
    }

```

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [February 12, 2024, 9:31am UTC](https://discuss.elastic.co/t/bulk-delete-elastic-search-client/353032/2 "2024-02-12T09:31:43Z")

</div>

Hi @Manoj_Upadhyay,

Are you receiving a particular error from the above code? Or is it not working in the way that you expect?

---

<div class="post-metadata">

**Author:** ![Manoj\_Upadhyay](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/manoj_upadhyay/32/60156_2.png) [@Manoj\_Upadhyay](https://discuss.elastic.co/u/Manoj_Upadhyay)\
**Post date:** [February 12, 2024, 6:06pm UTC](https://discuss.elastic.co/t/bulk-delete-elastic-search-client/353032/3 "2024-02-12T18:06:08Z")

</div>

Hi Carly,

After ES 8. x upgrade, am using ElasticsearchClient in place of RestHighClient and don't see an option to use deleteByQuery. Tried to read the document but saw only option the to search first and then all IDs in DeleteByQueryRequest then delete them.

---

<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:** [February 13, 2024, 3:24pm UTC](https://discuss.elastic.co/t/bulk-delete-elastic-search-client/353032/4 "2024-02-13T15:24:52Z")

</div>

Have a look at this example:

> <https://github.com/dadoonet/elasticsearch-java-client-demo/blob/d28fb1b977000a3243ea01579d2ae10016bacbab/src/test/java/fr/pilato/test/elasticsearch/hlclient/EsClientIT.java#L632-L652>

---

<div class="post-metadata">

**Author:** ![Manoj\_Upadhyay](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/manoj_upadhyay/32/60156_2.png) [@Manoj\_Upadhyay](https://discuss.elastic.co/u/Manoj_Upadhyay)\
**Post date:** [February 14, 2024, 12:22am UTC](https://discuss.elastic.co/t/bulk-delete-elastic-search-client/353032/5 "2024-02-14T00:22:48Z")

</div>

Thanks a lot. if there is any direct APIs(similar to ES 7) to delete documents in bulk in place of finding the search result, iterating hits, and adding them in DeleteByQueryRequest then deleting them in bulk.

---

<div class="post-metadata">

**Author:** ![Manoj\_Upadhyay](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/manoj_upadhyay/32/60156_2.png) [@Manoj\_Upadhyay](https://discuss.elastic.co/u/Manoj_Upadhyay)\
**Post date:** [March 4, 2024, 11:07pm UTC](https://discuss.elastic.co/t/bulk-delete-elastic-search-client/353032/6 "2024-03-04T23:07:01Z")

</div>

Thanks a lot for help and i got it

---

<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:** [April 1, 2024, 11:07pm UTC](https://discuss.elastic.co/t/bulk-delete-elastic-search-client/353032/7 "2024-04-01T23:07:24Z")

</div>

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