python - 定时清理ES 索引
只保留三天
#!/usr/bin/env python3 # -*- coding:utf-8 -*- import os import datetime # 时间转化为字符串 now_time = datetime.datetime.now().strptime(datetime.datetime.now().strftime("%Y.%m.%d"),"%Y.%m.%d") os.system("curl -XGET http://127.0.0.1:9200/_cat/indices > date.txt") with open("date.txt","r") as f: for line in f.readlines(): index = line.strip().split()[2] try: index_strftime = datetime.datetime.strptime(index.split("-")[-1], "%Y.%m.%d") Ca = (now_time - index_strftime) if str(Ca).split()[0] == "0:00:00": continue elif int(str(Ca).split()[0]) >= 3: command = "curl -XDELETE http://127.0.0.1:9200/%s" % index print(command) os.system(command) else: print(index,"no del") except: pass
加入到定时任务即可