55import time
66
77
8+ def delete_index (pc , index_name ):
9+ try :
10+ print ("Deleting index: " + index_name )
11+ desc = pc .describe_index (index_name )
12+
13+ # Check whether index can be deleted
14+ if desc .deletion_protection == "enabled" :
15+ pc .configure_index (index_name , deletion_protection = "disabled" )
16+
17+ # Wait for index to be ready before deleting
18+ ready_to_delete = False
19+ max_wait = 60
20+ time_waited = 0
21+ while not ready_to_delete :
22+ desc = pc .describe_index (index_name )
23+ if desc .status .state == "Ready" :
24+ ready_to_delete = True
25+ break
26+ else :
27+ print ("Index is not ready yet. Waiting for 2 seconds." )
28+ time .sleep (2 )
29+ time_waited += 2
30+
31+ if time_waited > max_wait :
32+ print (f"Timed out waiting for index { index_name } to be ready" )
33+ break
34+
35+ pc .delete_index (index_name )
36+ except Exception as e :
37+ print ("Failed to delete index: " + index_name + " " + str (e ))
38+ pass
39+
40+
841def delete_everything (pc ):
942 for collection in pc .list_collections ().names ():
1043 try :
@@ -15,36 +48,7 @@ def delete_everything(pc):
1548 pass
1649
1750 for index in pc .list_indexes ().names ():
18- try :
19- print ("Deleting index: " + index )
20- desc = pc .describe_index (index )
21-
22- # Check whether index can be deleted
23- if desc .deletion_protection == "enabled" :
24- pc .configure_index (index , deletion_protection = "disabled" )
25-
26- # Wait for index to be ready before deleting
27- ready_to_delete = False
28- max_wait = 60
29- time_waited = 0
30- while not ready_to_delete :
31- desc = pc .describe_index (index )
32- if desc .status .state == "Ready" :
33- ready_to_delete = True
34- break
35- else :
36- print ("Index is not ready yet. Waiting for 2 seconds." )
37- time .sleep (2 )
38- time_waited += 2
39-
40- if time_waited > max_wait :
41- print (f"Timed out waiting for index { index } to be ready" )
42- break
43-
44- pc .delete_index (index )
45- except Exception as e :
46- print ("Failed to delete index: " + index + " " + str (e ))
47- pass
51+ delete_index (pc , index )
4852
4953
5054def parse_date (resource_name ):
@@ -86,12 +90,7 @@ def delete_old(pc):
8690
8791 for index in pc .list_indexes ().names ():
8892 if is_resource_old (index ):
89- try :
90- print ("Deleting index: " + index )
91- pc .delete_index (index )
92- except Exception as e :
93- print ("Failed to delete index: " + index + " " + str (e ))
94- pass
93+ delete_index (pc , index )
9594 else :
9695 print ("Skipping index, not old enough: " + index )
9796
0 commit comments