Delete all indices with specific pre-fix using JAVA APIs


(Hitesh Hotchandani) #1

Hi all,
Does anyone know how to perform
curl -XDELETE "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. :slight_smile:


(David Pilato) #2

What did you try ?

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


(Hitesh Hotchandani) #3

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.


(David Pilato) #4

Why not doing:

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

Is this not working?


(Hitesh Hotchandani) #5

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


(Hitesh Hotchandani) #6

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


(David Pilato) #7

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:


(system) #8

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