A comprehensive, modular command-line tool for freeing up storage space and optimizing performance on macOS systems. This refactored version provides improved code organization, enhanced functionality, and better maintainability.
- 40% less code repetition - Reduced from 1383 to 1098 lines through modular functions
- Enhanced architecture - Reusable utility functions for common operations
- New cleanup features - APFS snapshots, swap monitoring, login items audit, and more
- Better tracking - Operation statistics and detailed logging
- Improved UX - Emoji indicators and clearer section organization
- Performance monitoring - Desktop organization check, memory analysis, swap usage tracking
- Interactive cleanup - Asks for confirmation before each operation
- Safe operations - Shows file sizes before deletion and warns about important data
- Comprehensive coverage - 30 different cleanup operations covering system and user files
- Visual feedback - Color-coded output with emoji indicators and progress tracking
- Before/after comparison - Shows storage space freed up
- Operation logging - Detailed logs saved to
/tmp/cleanup_log_YYYYMMDD.log - Statistics tracking - Track completed vs skipped operations
- Modular design - Easy to maintain and extend
- ⏰ Time Machine Local Snapshots - Local backup snapshots
- 📸 APFS Snapshots (NEW) - System update snapshots that persist after updates
- 💾 System Cache Files - System-level caches in
/Library/Caches/ - 👤 User Cache Files - User-specific caches in
~/Library/Caches/ - 📝 Log Files - System and user logs
- 🗑️ Temporary Files - System and user temporary files
- 📱 iOS Device Backups - iPhone/iPad backup files
- 🗑️ Trash - Empty the Trash folder
- 🌐 Browser Caches - Safari, Chrome, Firefox, Edge, Opera, Brave, Arc, Vivaldi
- 🎨 Professional Applications - Adobe CC, Final Cut Pro, Logic Pro, Sketch, Figma, etc.
- 📧 Mail & Downloads - Mail attachments and old downloads (>30 days)
- ☁️ Core Data & CloudKit - App sync caches
- 💭 Memory Purge - Free up inactive memory
- 💿 Swap Usage Analysis (NEW) - Monitor and analyze swap usage with recommendations
- 🔍 Spotlight Index - Rebuild for improved search performance
- 🔧 macOS Maintenance - Run Launch Services, dyld cache, and locate database updates
- 🔤 Font, QuickLook & DNS - Clear font, preview, and network caches
- 📦 Package Managers - npm, yarn, pnpm, bun, pip, conda, gem, cargo (all in one operation)
- 🍺 Homebrew - Update and clean Homebrew packages
- 🔨 Xcode & Developer Tools - DerivedData, DeviceSupport, Simulator data
- 🔐 Hidden Developer Caches - .npm, .gradle, .m2, .composer, .cargo, etc.
- 🐳 Docker - Containers, images, volumes, networks, build cache
- 🚀 Login Items Audit (NEW) - Show startup applications for performance optimization
- 🖥️ Desktop Organization Check (NEW) - Warn if too many desktop items (impacts performance)
- ⚙️ Orphaned Preferences (NEW) - Identify .plist files from uninstalled apps
- 🔄 macOS Updates Cache - Downloaded but uninstalled system updates
- 😴 Hibernation Sleep Image - Disable hibernation and remove sleep image
- 💿 macOS Installer Apps - Remove old system installer applications
- 📊 Large File Scanner - Find files >1GB in common locations
- 🔄 Purgeable Space Recovery - Trigger system cleanup of purgeable space
# Run with sudo for full functionality
sudo bash cleanup.shImportant: The script requires sudo privileges to access system-level files and perform comprehensive cleanup operations.
The script will:
- Show current disk space usage
- Present 30 cleanup operations one by one
- Display size information before each operation
- Ask for confirmation before deleting anything
- Track operations completed vs skipped
- Show final statistics and space freed
- Provide recommendations for ongoing maintenance
- Save detailed logs to
/tmp/cleanup_log_YYYYMMDD.log
Typical run time: 10-30 minutes (depending on selections and system size)
- Backup warning - Prominent reminder to backup important data before starting
- Confirmation prompts - Asks before each cleanup operation
- Double confirmation - Dangerous operations require typing "YES" in all caps
- Size display - Shows how much space each operation will free before deletion
- Non-destructive - Only removes cache, temporary, and log files (not user data)
- Selective cleaning - Skip any operation you're not comfortable with
- Operation logging - All actions logged with timestamps for audit trail
- Statistics tracking - See exactly how many operations were completed vs skipped
| Feature | cleanup.sh | CleanMyMac.app |
|---|---|---|
| Cost | Free and open source | Paid subscription (~$95/year) |
| Transparency | Fully visible operations | Black box operations |
| Control | Granular control over each operation | Limited customization |
| Safety | Manual confirmation for each step | Automated with some user control |
| Coverage | Comprehensive system cleanup | Broader feature set (malware scan, optimization) |
| Learning | Educational - shows what's being cleaned | User-friendly but less educational |
| Dependencies | Pure bash script | Requires app installation |
| Updates | Manual script updates | Automatic app updates |
- Complete transparency - You can see exactly what commands are being run
- Educational value - Learn about macOS file system and cleanup operations
- No subscription costs - Free to use and modify
- Scriptable - Can be automated or customized for specific needs
- Lightweight - No background processes or system monitoring
- Open source - Can be audited and modified as needed
- User-friendly GUI - More accessible for non-technical users
- Additional features - Malware detection, duplicate finder, app uninstaller
- Automated scheduling - Can run cleanups automatically
- Professional support - Customer service and regular updates
- System monitoring - Real-time storage and performance monitoring
- Integration - Better integration with macOS notifications and system
Use cleanup.sh when:
- You want full control over what gets cleaned
- You prefer command-line tools
- You want to understand what cleanup operations do
- You need a free solution
- You want to customize or script the cleanup process
Use CleanMyMac.app when:
- You prefer a graphical interface
- You want additional features like malware scanning
- You need automated, scheduled cleanups
- You don't mind paying for convenience
- You want professional support
- macOS (tested on macOS 10.14+)
- Sudo privileges
- Terminal access
- Download the
cleanup.shscript - Make it executable:
chmod +x cleanup.sh - Run with sudo:
sudo bash cleanup.sh
The refactored version includes modular functions for better code organization:
print_section()- Consistent section headers with emoji indicatorscalculate_size()- Calculate directory/file sizescalculate_size_bytes()- Size calculation for statistics trackingconfirm()/double_confirm()- User confirmation with safety levelslog_operation()- Timestamp-based operation logging
clean_cache_directory()- Generic cache cleaning with size trackingclean_multiple_caches()- Batch process arrays of cache pathsclean_browser_caches()- Unified browser cache handler (reduces 95 lines to 48)clean_package_managers()- Consolidated package manager cleanup
All paths centralized in configuration arrays:
BROWSER_CACHES- Associative array of browser cache pathsPROFESSIONAL_APP_CACHES- Professional application cachesPACKAGE_MANAGER_CACHES- Developer tool cachesHIDDEN_DEV_CACHES- Hidden developer directories
This script is open source and can be modified to suit your specific needs.
- Add cleanup locations to configuration arrays (lines 26-80)
- Create new cleanup functions using
clean_cache_directory()template - Modify confirmation behavior or warnings
- Adjust color schemes and emoji indicators
- Extend logging functionality
# Example: Add a new cache cleanup
print_section "New Cache Type"
clean_cache_directory "AppName" "/path/to/cache" "Optional warning message"- Use modular functions instead of repetitive code
- Add operations to configuration arrays when possible
- Include size display before cleanup
- Track operations in statistics
- Log all operations with timestamps
- Include emoji indicators in section headers
Always backup important data before running any cleanup operations. While this script is designed to be safe and only remove temporary/cache files, use at your own risk.