Delete multiple indices in one Elasticsearch HTTP request (cURL)

31,602

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
Share:
31,602
oHo
Author by

oHo

Excellent relational skills, both face to face and with a broader audience. Back-end developer loving Python, Go, C++, Linux, Ansible, Docker, Bar-metal hosting, InfluxDB. Experiences in stock market place, bank & finance, crypto-currencies, blockchain and open finance. Practitioner of self-governance: trusting & empowering my colleagues, and providing both alignment and autonomy. Good writing skills. Past hobbies: Organized C++ Meetups in Paris and on worldwide C++FRUG community Member of my city Greeter association and Hospitality Club too. Created website LittleMap.org to create/share libre maps for impair people. Wrote one piece of the NFC standards (at MasterCard) Developed an early Linux mobile, before Android release. Developed games on my old VG5000µ and CPC6128 computers.

Updated on July 09, 2022

Comments

  • oHo
    oHo almost 2 years

    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]:

    • to delete only my_index-YYYY.MM.dd
    • to keep my_index-YYYY.MM.dd-*

    The relevant Elasticsearch documentation I have found:

    • Delete index API does say nothing on regex.

    • Multiple indices says:

      It also support wildcards, for example: test* or *test or te*t or *test*, and the ability to "add" (+) and "remove" (-), for example: +test*,-test3.

    • Date math support in index names says:

      Almost all APIs that have an index parameter, support date math in the index parameter value.
      [...]
      date_format is the optional format in which the computed date should be rendered. Defaults to YYYY.MM.dd.


    My Questions:

    • Is it possible to send a DELETE request method to Elasticsearch HTTP server to delete indices only formatted my_index-YYYY.MM.dd?
    • Or the inverse, to delete all my_index-* but keeping my_index-*-*?

    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"
                }
            }
        }
    }'