5
5
import time
6
6
7
7
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
+
8
41
def delete_everything (pc ):
9
42
for collection in pc .list_collections ().names ():
10
43
try :
@@ -15,36 +48,7 @@ def delete_everything(pc):
15
48
pass
16
49
17
50
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 )
48
52
49
53
50
54
def parse_date (resource_name ):
@@ -86,12 +90,7 @@ def delete_old(pc):
86
90
87
91
for index in pc .list_indexes ().names ():
88
92
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 )
95
94
else :
96
95
print ("Skipping index, not old enough: " + index )
97
96
0 commit comments