#!/usr/bin/env python3 """ Script to delete all patients from the CPS database. This will also clean up related data like appointments, messages, analyses, etc. """ import asyncio import sys import os from pathlib import Path # Add the current directory to Python path sys.path.append(str(Path(__file__).parent)) from db.mongo import ( patients_collection, appointments_collection, messages_collection, patient_analysis_results_collection, chats_collection, clinical_alerts_collection, notifications_collection, db ) async def delete_all_patients(): """Delete all patients and related data from the database""" print("āš ļø WARNING: This will delete ALL patients and related data!") print("This action cannot be undone.") # Get confirmation confirm = input("\nType 'DELETE ALL' to confirm: ") if confirm != "DELETE ALL": print("Operation cancelled.") return try: print("\nšŸ”„ Starting deletion process...") # Get counts before deletion patient_count = await patients_collection.count_documents({}) appointment_count = await appointments_collection.count_documents({}) message_count = await messages_collection.count_documents({}) analysis_count = await patient_analysis_results_collection.count_documents({}) chat_count = await chats_collection.count_documents({}) alert_count = await clinical_alerts_collection.count_documents({}) notification_count = await notifications_collection.count_documents({}) print(f"šŸ“Š Current data counts:") print(f" - Patients: {patient_count}") print(f" - Appointments: {appointment_count}") print(f" - Messages: {message_count}") print(f" - Analysis Results: {analysis_count}") print(f" - Chats: {chat_count}") print(f" - Clinical Alerts: {alert_count}") print(f" - Notifications: {notification_count}") # Delete related data first print("\nšŸ—‘ļø Deleting related data...") # Delete appointments if appointment_count > 0: result = await appointments_collection.delete_many({}) print(f" āœ… Deleted {result.deleted_count} appointments") # Delete messages if message_count > 0: result = await messages_collection.delete_many({}) print(f" āœ… Deleted {result.deleted_count} messages") # Delete analysis results if analysis_count > 0: result = await patient_analysis_results_collection.delete_many({}) print(f" āœ… Deleted {result.deleted_count} analysis results") # Delete chats if chat_count > 0: result = await chats_collection.delete_many({}) print(f" āœ… Deleted {result.deleted_count} chats") # Delete clinical alerts if alert_count > 0: result = await clinical_alerts_collection.delete_many({}) print(f" āœ… Deleted {result.deleted_count} clinical alerts") # Delete notifications if notification_count > 0: result = await notifications_collection.delete_many({}) print(f" āœ… Deleted {result.deleted_count} notifications") # Finally, delete all patients print("\nšŸ‘„ Deleting all patients...") if patient_count > 0: result = await patients_collection.delete_many({}) print(f" āœ… Deleted {result.deleted_count} patients") # Verify deletion print("\nšŸ” Verifying deletion...") remaining_patients = await patients_collection.count_documents({}) remaining_appointments = await appointments_collection.count_documents({}) remaining_messages = await messages_collection.count_documents({}) if remaining_patients == 0 and remaining_appointments == 0 and remaining_messages == 0: print("āœ… All patients and related data have been successfully deleted!") else: print("āš ļø Some data may still remain:") print(f" - Remaining patients: {remaining_patients}") print(f" - Remaining appointments: {remaining_appointments}") print(f" - Remaining messages: {remaining_messages}") except Exception as e: print(f"āŒ Error during deletion: {str(e)}") raise async def main(): """Main function""" print("šŸ„ CPS Database Cleanup Tool") print("=" * 40) try: await delete_all_patients() except KeyboardInterrupt: print("\n\nā¹ļø Operation cancelled by user.") except Exception as e: print(f"\nāŒ Fatal error: {str(e)}") sys.exit(1) if __name__ == "__main__": asyncio.run(main())