DEV Community

Zeeshan Zaidi
Zeeshan Zaidi

Posted on

Delete elasticsearch index without Delete Index API over the TLS while using x-pack security

#!/bin/bash

DAYSAGO=$(date --date="1 day ago" +%Y%m%d)

https://localhost:9200/_cat/indices?v | grep 'filebeat'`
ALLLINES=`/usr/bin/curl -s -XGET -u 'elastic:Elastic@123' https://localhost:9200/_cat/indices?v | grep 'filebeat'`

echo
echo "This is what to be delete for ELK!!! You can not recover this one gone"
echo

echo "$ALLLINES" | while read LINE
do
#  FORMATEDLINE=`echo $LINE | awk '{ print $3 }' | awk -F'-' '{ print $4 }' | sed 's/\.//g' ` 
  FORMATEDLINE=`echo $LINE | awk '{ print $3 }' | awk -F'-' '{ print $(NF) }' | sed 's/\.//g' `
  if [[ "$FORMATEDLINE" -lt "$DAYSAGO" ]]
  then
    TODELETE=`echo $LINE | awk '{ print $3 }'`
    echo "https://localhost:9200/$TODELETE"
  fi
done

echo
echo -n "if this make sence, Y to continue N to exit [Y/N]:"
read INPUT
if [ "$INPUT" == "Y" ] || [ "$INPUT" == "y" ] || [ "$INPUT" == "yes" ] || [ "$INPUT" == "YES" ]
then
  echo "$ALLLINES" | while read LINE
  do
     FORMATEDLINE=`echo $LINE | awk '{ print $3 }' | awk -F'-' '{ print $(NF) }' | sed 's/\.//g' `
    if [[ "$FORMATEDLINE" -lt "$DAYSAGO" ]]
    then
      TODELETE=`echo $LINE | awk '{ print $3 }'`
      /usr/bin/curl -XDELETE -u 'elastic:Elastic@123' localhost:9200/$TODELETE
      sleep 1
      fi
  done
else 
  echo SCRIPT CLOSED BY USER, BYE ...
  echo
  exit
fi
Enter fullscreen mode Exit fullscreen mode

Top comments (0)