Skip to content

Instantly share code, notes, and snippets.

@ilyaevseev
Created June 4, 2019 16:23
Show Gist options
  • Save ilyaevseev/76d37e8cd5584976424c650e34d4cc76 to your computer and use it in GitHub Desktop.
Save ilyaevseev/76d37e8cd5584976424c650e34d4cc76 to your computer and use it in GitHub Desktop.
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