Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all index except one/some in Elasticsearch?

Is there any way to delete all indices except one?

We can use the metadata _index of document in a GET request:

GET _count
{
  "query": {
    "match": {
      "_index": "indexname"
    }
  }
}

The above query doesn't make sense but just to show that we can use _index inside a query I have mentioned it.

I have tried the below query, but I guess _all API doesn't support query.

DELETE _all
{
  "query" : {
  "bool" : {
   "must_not" : [
     {
       "match": {
         "_index": "indexname"
       }
     }
   ]
  }
 }
}

Is there any way to delete all indices except one/some without using bulk API ?

like image 837
TechnocratSid Avatar asked Dec 18 '22 21:12

TechnocratSid


1 Answers

Try to use multiple indices syntax. You can specify all indices with * and then exclude some of them with -.

Suppose we need to remove all indices except foo and bar, so the HTTP request should be

curl -X DELETE -i 'http://{server}:{port}/*,-foo,-bar'
like image 119
Alexey Prudnikov Avatar answered Jan 14 '23 18:01

Alexey Prudnikov