@@ -100,14 +100,50 @@ async def cleanup():
100
100
click .echo (f"Index { index_name } does not exist" )
101
101
return
102
102
103
+ # Define query based on the cleanup type
103
104
if test_docs_only :
104
- await output . cleanup_test_documents ( index_name )
105
- click . echo ( f"Cleaned up test documents from index { index_name } " )
105
+ query = { "query" : { "term" : { "test_document" : True }}}
106
+ operation_desc = " test documents"
106
107
else :
107
- output .es .delete_by_query (
108
- index = index_name , body = {"query" : {"match_all" : {}}}
108
+ query = {"query" : {"match_all" : {}}}
109
+ operation_desc = "documents"
110
+
111
+ try :
112
+ # First count how many documents will be affected
113
+ count_result = output .es .count (index = index_name , body = query )
114
+ doc_count = count_result ["count" ]
115
+
116
+ # Ask for confirmation
117
+ if not click .confirm (
118
+ f"\n Warning: { doc_count } { operation_desc } will be deleted from index '{ index_name } '. Do you want to continue?"
119
+ ):
120
+ click .echo ("Operation cancelled" )
121
+ return
122
+
123
+ # Proceed with deletion
124
+ delete_result = output .es .delete_by_query (
125
+ index = index_name , body = query
109
126
)
110
- click .echo (f"Removed all documents from index { index_name } " )
127
+
128
+ # Print detailed deletion results
129
+ click .echo ("\n Deletion Results:" )
130
+ click .echo (
131
+ f"Total { operation_desc } deleted: { delete_result ['deleted' ]} "
132
+ )
133
+ click .echo (f"Total batches: { delete_result ['batches' ]} " )
134
+ click .echo (f"Documents that failed: { delete_result ['failures' ]} " )
135
+ click .echo (f"Time taken: { delete_result ['took' ]} ms" )
136
+
137
+ if delete_result .get ("failures" ):
138
+ click .echo ("\n Failures encountered:" )
139
+ for failure in delete_result ["failures" ]:
140
+ click .echo (f"Document ID: { failure ['_id' ]} " )
141
+ click .echo (f"Error: { failure .get ('error' )} " )
142
+ click .echo ("---" )
143
+
144
+ except Exception as e :
145
+ click .echo (f"Error during cleanup: { e } " , err = True )
146
+ raise click .ClickException (str (e ))
111
147
112
148
return run_in_reactor (cleanup ())
113
149
0 commit comments