Skip to content

Instantly share code, notes, and snippets.

@baybatu
Created June 10, 2021 07:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baybatu/3012dc91924c35a5d7b8a09f54dc3a49 to your computer and use it in GitHub Desktop.
Save baybatu/3012dc91924c35a5d7b8a09f54dc3a49 to your computer and use it in GitHub Desktop.
delete index by pattern in Elasticsearch with Python
# usage
# python delete-index-by-pattern-in-es.py http://ES_HOST:9200 my-es-index-*
# indexes matched with pattern you entered will be deleted
from elasticsearch import Elasticsearch
import sys
es_host = sys.argv[1]
index_pattern = sys.argv[2]
print(f"es_host:{es_host} ; index_pattern:{index_pattern}")
es = Elasticsearch([es_host])
for index_name in es.indices.get(index_pattern):
print(f"Trying to delete es index : {index_name}")
res = es.indices.delete(index=index_name, ignore=[400, 404])
print(f"IndexName:{index_name} ; ES response:{res}")
print("DONE")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment