Complete Cleanup Workflow
Gmail Cleanup โ Practical Scripting
Course 1 ยท Chapter 5 ยท Complete Cleanup Workflow
๐ Complete Cleanup Workflow
Now it's time to bring everything together: configuration, API connection, utility functions, and safety checks. This chapter teaches you how to orchestrate a complete, safe cleanup workflow that finds emails, previews them, and deletes or archives them with full reporting.
๐ The Cleanup Workflow
Step 1: Load Configuration
Read .env file and validate settings
Step 2: Authenticate
Connect to Gmail API using OAuth
Step 3: Find Emails
Query for emails matching cleanup criteria
Step 4: Preview Results
Display what will be deleted (safety check!)
Step 5: Confirm Action
Ask user before deleting (never automatic!)
Step 6: Execute Cleanup
Delete or archive emails
Step 7: Report Results
Show what was deleted and storage freed
๐ง Main Cleanup Script
import sys from config import Config from gmail_utils import get_gmail_service, GmailCleanup def preview_emails(emails: list, cleanup: GmailCleanup, limit: int = 5): """Show sample of emails to be deleted.""" print("\n๐ PREVIEW (first 5):") print("-" * 80) for email in emails[:limit]: details = cleanup.get_email_details(email['id']) if details: print(f"Subject: {details['subject']}") print(f"From: {details['from']}") print(f"Date: {details['date']}") print(f"Size: {details['size_mb']:.2f} MB") print("-" * 80) def confirm_action(email_count: int) -> bool: """Ask user to confirm deletion.""" print(f"\nโ ๏ธ About to delete {email_count} emails!") response = input("Type 'yes' to confirm: ") return response.lower() == 'yes' def main(): """Main cleanup workflow.""" print("=" * 80) print("๐ Gmail Cleanup Utility") print("=" * 80) # Step 1: Load configuration print("\n๐ Loading configuration...") config = Config() if not config.validate(): return False # Step 2: Connect to Gmail print("๐ Connecting to Gmail...") service = get_gmail_service() cleanup = GmailCleanup(service, config) # Step 3: Find old emails print(f"\n๐ Finding emails older than {config.days_old} days...") emails = cleanup.find_old_emails(config.days_old) if not emails: print("โ No old emails found!") return True print(f"โ Found {len(emails)} emails") # Step 4: Preview results preview_emails(emails, cleanup) # Step 5: Confirm action if not confirm_action(len(emails)): print("โ Cleanup cancelled") return False # Step 6: Execute cleanup print("\n๐๏ธ Deleting emails...") for email in emails: cleanup.delete_email(email['id']) # Step 7: Report results stats = cleanup.get_stats() print("\n" + "=" * 80) print("โ CLEANUP COMPLETE!") print("=" * 80) print(f"Emails deleted: {stats['deleted']}") print(f"Storage freed: {stats['storage_freed_mb']:.2f} MB") return True if __name__ == "__main__": success = main() sys.exit(0 if success else 1)
โ ๏ธ Safety Features
- โ Configuration validation (catches errors early)
- โ Preview of emails before deletion (visual confirmation)
- โ User confirmation prompt (prevents accidents)
- โ Error handling throughout (graceful failures)
- โ Comprehensive reporting (what happened)
๐ป Coding Challenges
Challenge 1: Safe Deletion
Create a workflow that:
- Finds 10 old emails
- Displays subject, sender, date for preview
- Asks user to confirm (input validation)
- Deletes only if confirmed
Goal: Build safe, user-friendly deletion.
Challenge 2: Multi-Criteria Cleanup
Build cleanup that finds and deletes:
- Emails older than 365 days
- Emails with large attachments
- Emails from specific senders
Goal: Handle multiple cleanup criteria.
Challenge 3: Complete Workflow
Build the full cleanup script with:
- Configuration loading and validation
- Gmail authentication
- Finding and previewing emails
- User confirmation
- Deletion and reporting
Goal: End-to-end cleanup automation.