Skip to content

ggasp/cleanup

Repository files navigation

MacBook Storage Cleanup Script (Refactored)

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.

✨ What's New in the Refactored Version

  • 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

Features

  • 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

What It Cleans (30 Operations)

Core System Cleanup

  1. ⏰ Time Machine Local Snapshots - Local backup snapshots
  2. 📸 APFS Snapshots (NEW) - System update snapshots that persist after updates
  3. 💾 System Cache Files - System-level caches in /Library/Caches/
  4. 👤 User Cache Files - User-specific caches in ~/Library/Caches/
  5. 📝 Log Files - System and user logs
  6. 🗑️ Temporary Files - System and user temporary files
  7. 📱 iOS Device Backups - iPhone/iPad backup files
  8. 🗑️ Trash - Empty the Trash folder

Browser & Application Caches

  1. 🌐 Browser Caches - Safari, Chrome, Firefox, Edge, Opera, Brave, Arc, Vivaldi
  2. 🎨 Professional Applications - Adobe CC, Final Cut Pro, Logic Pro, Sketch, Figma, etc.
  3. 📧 Mail & Downloads - Mail attachments and old downloads (>30 days)
  4. ☁️ Core Data & CloudKit - App sync caches

Performance & Memory

  1. 💭 Memory Purge - Free up inactive memory
  2. 💿 Swap Usage Analysis (NEW) - Monitor and analyze swap usage with recommendations
  3. 🔍 Spotlight Index - Rebuild for improved search performance
  4. 🔧 macOS Maintenance - Run Launch Services, dyld cache, and locate database updates
  5. 🔤 Font, QuickLook & DNS - Clear font, preview, and network caches

Developer Tools

  1. 📦 Package Managers - npm, yarn, pnpm, bun, pip, conda, gem, cargo (all in one operation)
  2. 🍺 Homebrew - Update and clean Homebrew packages
  3. 🔨 Xcode & Developer Tools - DerivedData, DeviceSupport, Simulator data
  4. 🔐 Hidden Developer Caches - .npm, .gradle, .m2, .composer, .cargo, etc.
  5. 🐳 Docker - Containers, images, volumes, networks, build cache

System Optimization

  1. 🚀 Login Items Audit (NEW) - Show startup applications for performance optimization
  2. 🖥️ Desktop Organization Check (NEW) - Warn if too many desktop items (impacts performance)
  3. ⚙️ Orphaned Preferences (NEW) - Identify .plist files from uninstalled apps
  4. 🔄 macOS Updates Cache - Downloaded but uninstalled system updates
  5. 😴 Hibernation Sleep Image - Disable hibernation and remove sleep image
  6. 💿 macOS Installer Apps - Remove old system installer applications

Analysis Tools

  1. 📊 Large File Scanner - Find files >1GB in common locations
  2. 🔄 Purgeable Space Recovery - Trigger system cleanup of purgeable space

Usage

# Run with sudo for full functionality
sudo bash cleanup.sh

Important: The script requires sudo privileges to access system-level files and perform comprehensive cleanup operations.

What to Expect

The script will:

  1. Show current disk space usage
  2. Present 30 cleanup operations one by one
  3. Display size information before each operation
  4. Ask for confirmation before deleting anything
  5. Track operations completed vs skipped
  6. Show final statistics and space freed
  7. Provide recommendations for ongoing maintenance
  8. Save detailed logs to /tmp/cleanup_log_YYYYMMDD.log

Typical run time: 10-30 minutes (depending on selections and system size)

Safety Features

  • 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

Comparison with CleanMyMac.app

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

Advantages of cleanup.sh

  • 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

Advantages of CleanMyMac.app

  • 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

When to Use Each

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

Requirements

  • macOS (tested on macOS 10.14+)
  • Sudo privileges
  • Terminal access

Installation

  1. Download the cleanup.sh script
  2. Make it executable: chmod +x cleanup.sh
  3. Run with sudo: sudo bash cleanup.sh

Architecture Improvements

The refactored version includes modular functions for better code organization:

Core Utility Functions

  • print_section() - Consistent section headers with emoji indicators
  • calculate_size() - Calculate directory/file sizes
  • calculate_size_bytes() - Size calculation for statistics tracking
  • confirm() / double_confirm() - User confirmation with safety levels
  • log_operation() - Timestamp-based operation logging

Cleanup Functions

  • clean_cache_directory() - Generic cache cleaning with size tracking
  • clean_multiple_caches() - Batch process arrays of cache paths
  • clean_browser_caches() - Unified browser cache handler (reduces 95 lines to 48)
  • clean_package_managers() - Consolidated package manager cleanup

Configuration

All paths centralized in configuration arrays:

  • BROWSER_CACHES - Associative array of browser cache paths
  • PROFESSIONAL_APP_CACHES - Professional application caches
  • PACKAGE_MANAGER_CACHES - Developer tool caches
  • HIDDEN_DEV_CACHES - Hidden developer directories

Contributing

This script is open source and can be modified to suit your specific needs.

Common Customizations

  • 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

Adding New Cleanup Operations

# Example: Add a new cache cleanup
print_section "New Cache Type"
clean_cache_directory "AppName" "/path/to/cache" "Optional warning message"

Code Quality Standards

  • 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

Disclaimer

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.

About

A comprehensive command-line tool for freeing up storage space on macOS systems. This script provides an interactive, safe, and thorough approach to cleaning your Mac.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages