I have a script that deletes elasticsearch indices with doc count 0, which are not elasticsearch service indexes (indices names beginning with “.”)
Right now I can list the indices, prior to deletion, then the act of deletion happens one at a time.
The issue I am facing is that I want to push the list of indices into a delete request where in theory I will be acting a DELETE request of removing more than one index at a time e.g.:
DELETE /test-index-000001,test-index-000002,test-index-000003
At the moment, what I am struggling with is that my list gets created in the response url as one url:
https://dev-esp-cluster.cw-eng-test.com/[‘test-index-000001’,
‘test-index-000002’, ‘test-index-000003’]
However I think the correct method would be to create a list of delete urls e.g.
https://dev-esp-cluster.cw-eng-test.com/test-index-000001, https://dev-esp-cluster.cw-eng-test.com/test-index-000002, https://dev-esp-cluster.cw-eng-test.com/test-index-000003
So would I need to create an extra list/array of delete urls?
Here is the code:
content = json.loads(getRequestElasticSearch.text)
indices_to_delete = []
for index in content:
if int(index['docs.count']) == 0 and not index['index'].startswith("."):
indices_to_delete.append(index['index'])
print("Indices to be deleted:")
for idx in indices_to_delete:
print(idx)
proceed = input("Do you want to proceed with the deletion of these indices? (y/n): ")
if proceed.lower() != "y":
print("Deletion aborted by user.")
return
else:
elastic_console_delete = f"https://{configuration['ELASTIC_SEARCH_URL']}/"
delete_url = f"{elastic_console_delete}{indices_to_delete}"
print(delete_url)
response = requests.delete(delete_url, auth=(elastic_username, elastic_password))
if response.status_code == 200:
print("index deleted -" , "index name:" , index['index'] , ", doc.count:" , index['docs.count'] , ", elasticsearch url index:" , delete_url)
if response.status_code != 200:
print ("index not deleted -" , "index name:" , index['index'] , ", Reason:" , response.content)