Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Delete old time-based indexes from ElasticSearch
#!/bin/sh
MAX_DAYS="30"
ES_ADDR="127.0.0.1"
D0="$(date -d "$MAX_DAYS days ago" +%s)"
curl -sS "http://$ES_ADDR:9200/_cat/indices" |
awk 'match($3, /-([0-9][0-9][0-9][0-9])[\.\-]([0-9][0-9])[\.\-]([0-9][0-9])$/, m) { printf "%s-%s-%s %s\n", m[1],m[2],m[3],$3 }' |
while read D1 idx; do
D2="$(date -d "$D1" +%s 2>/dev/null || :)"
test "$D2" = "" && continue
test "$D2" -ge "$D0" && continue
result="$(curl -sS -X DELETE "http://$ES_ADDR:9200/$idx")"
test "$result" = '{"acknowledged":true}' && continue
echo "FAILED: $idx: $result"
exit 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment