oHo
oHo

Reputation: 54561

Delete multiple indices in one Elasticsearch HTTP request (cURL)

I was using this curl command line to clean my indices:

curl -XDELETE http://example.com/my_index-*

But, now, I want to delete my_index-.*[.][0-3][0-9]:


The relevant Elasticsearch documentation I have found:


My Questions:


For example, regex can sometimes be provided within the POST data:

curl -XPOST http://example.com/my_index-2017.07.14/_search?pretty' -H 'Content-Type: application/json' -d'
{
    "suggest": {
        "song-suggest" : {
            "regex" : "n[ever|i]r",
            "completion" : {
                "field" : "suggest"
            }
        }
    }
}'

Upvotes: 24

Views: 51063

Answers (1)

oHo
oHo

Reputation: 54561

Short answer

Delete all indices my_index-* except indices my_index-*-*

curl -X DELETE http://es.example.com/my_index-*,-my_index-*-*

No regex

Elasticsearch 5.x does not accept regex or filename patterns ?[a-z] to select multiple indices.

However, the multiple indices documentation allows + and - to include and exclude indices.

Script to prevent accidental deletion of indices my_index-*-*:

#!/bin/bash -xe
pattern="${1:-*}"
curl -X DELETE https://es.example.com/my_index-"$pattern",-my_index-*-*?pretty

Explanation

  • The parameter index can contain a comma separated list of index patterns, for example my_index_1,my_index_2,my_index_3.
  • Index pattern is based on wildcards, for example my_index*.
  • To include and exclude indices, use + and - as index prefix, for example my_index_*,-my_index_2017*,+my_index_2017-01*,-my_index_2017-01-31.
  • Do not need to use + on first index

Described example

This DELETE request deletes all indices my_index_* before my_index_2017-01-31

index_list='my_index_*,-my_index_2017*,+my_index_2017-01*,-my_index_2017-01-31'
curl -X DELETE http://es.example.com/"$index_list"
  • Delete all my_index_*
  • Except my_index_2017*
  • Delete my_index_2017-01*
  • Except my_index_2017-01-31

Upvotes: 48

Related Questions