Skip to content

Instantly share code, notes, and snippets.

@h1k3r
Created February 3, 2021 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h1k3r/5fd14f5c0d7911e8c27b4f975c1e1806 to your computer and use it in GitHub Desktop.
Save h1k3r/5fd14f5c0d7911e8c27b4f975c1e1806 to your computer and use it in GitHub Desktop.
elastic_search_delete_empty_indexes.sh
#!/bin/bash
# Based on https://discuss.elastic.co/t/finding-and-deleting-empty-indexes/172764
# Usage:
# elastic_search_delete_empty_indexes.sh servername:9200 #for dry run
# elastic_search_delete_empty_indexes.sh servername:9200 delete #to really delete
srv=""
delete=false
# Parameters check
if [ -z "$1" ]
then
echo "Pass server name and port as a first parameter."
exit
else
srv="$1"
fi
if [ "$2" == "delete" ]; then
echo "Deleting indexes."
delete=true
else
echo "Dry run. Pass delete as second parameter to delete for real."
fi
echo "Cleaning elastic $srv"
for idx in $(curl -XGET "http://$srv/_all/_settings" -s | jq 'keys' | sed 's/["|,]//g' | grep -E '[[:alpha:]]+'); do
count=$(curl -X GET "http://$srv/$idx/_count" -s | jq '.count')
if (( $count == 0 )); then
echo "$(tput setaf 2)** Index $idx is empty ... delete it$(tput setaf 7)"
if [ "$delete" == true ]; then
curl -XDELETE "http://$srv/$idx"
echo ""
fi
else
echo "$(tput setaf 1)Count $idx -> $count$(tput setaf 7)"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment