# Delete all indices with specific pre-fix using JAVA APIs

**URL:** <https://discuss.elastic.co/t/delete-all-indices-with-specific-pre-fix-using-java-apis/102738>\
**Category:** Elasticsearch\
**Created:** [October 4, 2017, 5:16pm UTC](https://discuss.elastic.co/t/delete-all-indices-with-specific-pre-fix-using-java-apis/102738 "2017-10-04T17:16:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hitesh-inbetween](https://avatars.discourse-cdn.com/v4/letter/h/51bf81/32.png) [@hitesh-inbetween](https://discuss.elastic.co/u/hitesh-inbetween)\
**Post date:** [October 4, 2017, 5:16pm UTC](https://discuss.elastic.co/t/delete-all-indices-with-specific-pre-fix-using-java-apis/102738/1 "2017-10-04T17:16:32Z")

</div>

Hi all,  
Does anyone know how to perform  
curl -XDELETE "[http://localhost:9200/test\_\*;](http://localhost:9200/test_*;)"  
via JAVA APIs?

I found the way to delete a particular index, but I have multiple indices with a particular prefix and a special event requires me to delete all the indexes with that particular prefix, since the data will be invalid for those indexes.

Any help appreciated. 🙂

---

<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:** [October 4, 2017, 5:59pm UTC](https://discuss.elastic.co/t/delete-all-indices-with-specific-pre-fix-using-java-apis/102738/2 "2017-10-04T17:59:19Z")

</div>

What did you try ?

If it’s one time only operation why not doing that with the rest api?

---

<div class="post-metadata">

**Author:** ![hitesh-inbetween](https://avatars.discourse-cdn.com/v4/letter/h/51bf81/32.png) [@hitesh-inbetween](https://discuss.elastic.co/u/hitesh-inbetween)\
**Post date:** [October 5, 2017, 8:25am UTC](https://discuss.elastic.co/t/delete-all-indices-with-specific-pre-fix-using-java-apis/102738/3 "2017-10-05T08:25:41Z")

</div>

Hi,  
I tried the following thing,

```
client = elasticSearchConnection.getClient();
client.admin().indices().delete(new DeleteIndexRequest(indexName)).actionGet();

```

Here the problem is that I'll have to fetch all the indices, and check the index name for particular index prefix and if it qualifies to be deleted, then only delete it.  
But as i was thinking, If it is possible to delete using wildcard entries, why not use it.

Also, I'm not much inclined to use the rest as I already have the client object and adding REST will add another flow of code and add maintenance overhead.  
But REST would be the last fall-through if nothing works.

---

<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:** [October 5, 2017, 8:51am UTC](https://discuss.elastic.co/t/delete-all-indices-with-specific-pre-fix-using-java-apis/102738/4 "2017-10-05T08:51:16Z")

</div>

Why not doing:

```auto
client.admin().indices().delete(new DeleteIndexRequest("test_*")).actionGet();

```

Is this not working?

---

<div class="post-metadata">

**Author:** ![hitesh-inbetween](https://avatars.discourse-cdn.com/v4/letter/h/51bf81/32.png) [@hitesh-inbetween](https://discuss.elastic.co/u/hitesh-inbetween)\
**Post date:** [October 5, 2017, 8:53am UTC](https://discuss.elastic.co/t/delete-all-indices-with-specific-pre-fix-using-java-apis/102738/5 "2017-10-05T08:53:00Z")

</div>

I was just about to try that.  
I'll share the outcome in a bit.  
Thanks 🙂

---

<div class="post-metadata">

**Author:** ![hitesh-inbetween](https://avatars.discourse-cdn.com/v4/letter/h/51bf81/32.png) [@hitesh-inbetween](https://discuss.elastic.co/u/hitesh-inbetween)\
**Post date:** [October 5, 2017, 9:01am UTC](https://discuss.elastic.co/t/delete-all-indices-with-specific-pre-fix-using-java-apis/102738/6 "2017-10-05T09:01:22Z")

</div>

Hi @dadoonet  
That works.  
It was stupid of me to not try that before posting here.  
Thanks 🙂

---

<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:** [October 5, 2017, 9:14am UTC](https://discuss.elastic.co/t/delete-all-indices-with-specific-pre-fix-using-java-apis/102738/7 "2017-10-05T09:14:26Z")

</div>

No worries.

In case of doubt, just try to look at the REST class in elasticsearch code.  
You'll see basically what is done behind the scene. Like here:

> <https://github.com/elastic/elasticsearch/blob/master/core/src/main/java/org/elasticsearch/rest/action/admin/indices/RestDeleteIndexAction.java#L46-L53>

---

<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:** [November 2, 2017, 9:14am UTC](https://discuss.elastic.co/t/delete-all-indices-with-specific-pre-fix-using-java-apis/102738/8 "2017-11-02T09:14:42Z")

</div>

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