Delete multiple indices in one Elasticsearch HTTP request (cURL) Delete multiple indices in one Elasticsearch HTTP request (cURL) elasticsearch elasticsearch

Delete multiple indices in one Elasticsearch HTTP request (cURL)


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 -xepattern="${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