#!/usr/bin/env python3 """ Clear all data from the SharePoint connector application. This script will: 1. Drop all vector store tables (PostgreSQL) 2. Delete all DynamoDB tables (connections, tokens, etc.) 3. Clear stored credentials """ import os import sys import boto3 from storage.credentials_storage import get_credentials_storage def clear_postgresql_vector_store(): """Drop all vector store tables from PostgreSQL.""" try: import psycopg2 # Get connection params from environment db_host = os.getenv("POSTGRES_HOST", "localhost") db_port = os.getenv("POSTGRES_PORT", "5432") db_name = os.getenv("POSTGRES_DB", "sharepoint_vectors") db_user = os.getenv("POSTGRES_USER", "postgres") db_password = os.getenv("POSTGRES_PASSWORD", "postgres") print(f"๐Ÿ“Š Connecting to PostgreSQL at {db_host}:{db_port}/{db_name}...") conn = psycopg2.connect( host=db_host, port=db_port, database=db_name, user=db_user, password=db_password ) cursor = conn.cursor() # Get table prefix table_prefix = os.getenv("TABLE_PREFIX", "dev_") # Drop tables in correct order (child tables first) tables = [ f"{table_prefix}document_tags", f"{table_prefix}document_chunks", f"{table_prefix}documents" ] for table in tables: try: cursor.execute(f"DROP TABLE IF EXISTS {table} CASCADE") print(f" โœ… Dropped table: {table}") except Exception as e: print(f" โš ๏ธ Could not drop {table}: {e}") conn.commit() cursor.close() conn.close() print("โœ… PostgreSQL vector store cleared") return True except ImportError: print("โš ๏ธ psycopg2 not installed, skipping PostgreSQL cleanup") return False except Exception as e: print(f"โŒ Error clearing PostgreSQL: {e}") return False def clear_dynamodb_tables(): """Delete all DynamoDB tables.""" try: # Determine if using local or AWS DynamoDB endpoint_url = os.getenv("DYNAMODB_ENDPOINT", "http://localhost:8000") region = os.getenv("AWS_REGION", "ap-southeast-2") table_prefix = os.getenv("TABLE_PREFIX", "dev_") if "localhost" in endpoint_url or "127.0.0.1" in endpoint_url: print(f"๐Ÿ“Š Connecting to LOCAL DynamoDB at {endpoint_url}...") else: print(f"๐Ÿ“Š Connecting to AWS DynamoDB in {region}...") dynamodb = boto3.client( 'dynamodb', region_name=region, endpoint_url=endpoint_url if "localhost" in endpoint_url or "127.0.0.1" in endpoint_url else None ) # List all tables response = dynamodb.list_tables() tables = response.get('TableNames', []) # Filter tables with our prefix our_tables = [t for t in tables if t.startswith(table_prefix)] if not our_tables: print(f" โ„น๏ธ No tables found with prefix '{table_prefix}'") return True print(f" Found {len(our_tables)} tables to delete:") for table in our_tables: print(f" - {table}") # Delete each table for table in our_tables: try: dynamodb.delete_table(TableName=table) print(f" โœ… Deleted table: {table}") except Exception as e: print(f" โš ๏ธ Could not delete {table}: {e}") print("โœ… DynamoDB tables cleared") return True except Exception as e: print(f"โŒ Error clearing DynamoDB: {e}") return False def clear_credentials(): """Clear all stored credentials.""" try: print("๐Ÿ“Š Clearing stored credentials...") storage = get_credentials_storage() # Get the storage file path if hasattr(storage, 'file_path'): file_path = storage.file_path if os.path.exists(file_path): os.remove(file_path) print(f" โœ… Deleted credentials file: {file_path}") else: print(f" โ„น๏ธ No credentials file found at: {file_path}") else: print(" โ„น๏ธ Credentials storage does not use file system") print("โœ… Credentials cleared") return True except Exception as e: print(f"โŒ Error clearing credentials: {e}") return False def main(): """Main function to clear all data.""" print("=" * 60) print("๐Ÿงน CLEARING ALL SHAREPOINT CONNECTOR DATA") print("=" * 60) print() # Check for --force flag force = '--force' in sys.argv or '-f' in sys.argv if not force: try: response = input("โš ๏ธ This will DELETE ALL data. Are you sure? (yes/no): ") if response.lower() not in ['yes', 'y']: print("โŒ Aborted") return except EOFError: print("\nโŒ No input received. Use --force to skip confirmation.") return print() print("Starting cleanup...") print() # Clear PostgreSQL vector store print("1๏ธโƒฃ Clearing PostgreSQL Vector Store...") clear_postgresql_vector_store() print() # Clear DynamoDB tables print("2๏ธโƒฃ Clearing DynamoDB Tables...") clear_dynamodb_tables() print() # Clear credentials print("3๏ธโƒฃ Clearing Stored Credentials...") clear_credentials() print() print("=" * 60) print("โœ… ALL DATA CLEARED SUCCESSFULLY") print("=" * 60) print() print("You can now start fresh by running the application:") print(" python app_dev.py") print() if __name__ == "__main__": main()