Skip to content

Commit

Permalink
a script to delete old ES indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
dsschult committed Oct 11, 2024
1 parent 43fd539 commit 94b4121
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions delete_old_indexes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import argparse
import re
import requests
from pprint import pprint


def list_indexes(host):
ret = requests.get(f'{host}/_cat/indices')
ret.raise_for_status()
indexes = []
for line in ret.text.split('\n'):
try:
if line.strip():
indexes.append(line.split()[2])
except Exception:
print('line:',line)
raise
return indexes

def delete_index(host, index, dry=False):
print('deleting index', index)
if not dry:
ret = requests.delete(f'{host}/{index}')
ret.raise_for_status()

def main():
parser = argparse.ArgumentParser('delete elasticsearch indexes')
parser.add_argument('--host', default='http://elk-1.icecube.wisc.edu:9200', help='elasticsearch host')
parser.add_argument('index_pattern', help='index regex pattern')
parser.add_argument('--dry-run', action='store_true', help='dry run')
args = parser.parse_args()

indexes = list_indexes(args.host)
print(args.index_pattern)
for index in indexes:
if re.match(args.index_pattern, index):
delete_index(args.host, index, dry=args.dry_run)



if __name__ == '__main__':
main()

0 comments on commit 94b4121

Please sign in to comment.