#!/bin/bash # Force Free Space - Remove APFS and Time Machine Snapshots # This script removes snapshots that are holding onto deleted file space set -e echo "=== Force Free Space - Remove Snapshots ===" echo "" echo "This script will remove APFS and Time Machine snapshots that may be" echo "holding onto space from deleted files." echo "" # Check current free space FREE_SPACE_BEFORE=$(df / | tail -1 | awk '{print $4}') FREE_SPACE_GB_BEFORE=$(echo "scale=2; $FREE_SPACE_BEFORE / 1024 / 1024" | bc) echo "Current free space: ${FREE_SPACE_GB_BEFORE}GB" echo "" # List current snapshots echo "📸 Current Snapshots:" echo "" echo "1. Time Machine Local Snapshots:" TM_SNAPSHOTS=$(tmutil listlocalsnapshots / 2>/dev/null | grep -v "Snapshots for disk" | grep -v "^$") if [ -n "$TM_SNAPSHOTS" ]; then echo "$TM_SNAPSHOTS" | while read snapshot; do if [ -n "$snapshot" ]; then echo " - $snapshot" fi done else echo " None found" fi echo "" echo "2. APFS System Snapshots:" APFS_SNAPSHOTS=$(diskutil apfs listSnapshots / 2>/dev/null | grep -E "Snapshot Name|Snapshot UUID" | head -10) if [ -n "$APFS_SNAPSHOTS" ]; then echo "$APFS_SNAPSHOTS" else echo " None found (or cannot list)" fi echo "" # Ask for confirmation read -p "Do you want to delete Time Machine local snapshots? (y/n) " -n 1 -r echo "" if [[ $REPLY =~ ^[Yy]$ ]]; then echo "" echo "🗑️ Deleting Time Machine local snapshots..." # Delete each snapshot echo "$TM_SNAPSHOTS" | while read snapshot; do if [ -n "$snapshot" ]; then echo " Deleting: $snapshot" sudo tmutil deletelocalsnapshots "$snapshot" 2>/dev/null && \ echo " ✓ Deleted $snapshot" || \ echo " ⚠️ Could not delete $snapshot (may be in use)" fi done # Also try to delete all at once sudo tmutil deletelocalsnapshots / 2>/dev/null || true echo " ✓ Time Machine snapshots cleanup attempted" fi echo "" # Force purge of purgeable space echo "3. Purging Purgeable Space..." echo " This forces macOS to actually free up space from deleted files." echo "" read -p "Do you want to purge purgeable space? (y/n) " -n 1 -r echo "" if [[ $REPLY =~ ^[Yy]$ ]]; then echo "" echo " Running purge command..." # Method 1: Use purge command (if available) if command -v purge &> /dev/null; then sudo purge echo " ✓ Purge command executed" fi # Method 2: Disable and re-enable local snapshots to force cleanup echo " Disabling local snapshots..." sudo tmutil disablelocal 2>/dev/null || true sleep 2 echo " Re-enabling local snapshots..." sudo tmutil enablelocal 2>/dev/null || true # Method 3: Force Time Machine to clean up echo " Triggering Time Machine cleanup..." sudo tmutil thinlocalsnapshots / 1000000000 4 2>/dev/null || true echo " ✓ Purge operations completed" fi echo "" # Check new free space sleep 2 FREE_SPACE_AFTER=$(df / | tail -1 | awk '{print $4}') FREE_SPACE_GB_AFTER=$(echo "scale=2; $FREE_SPACE_AFTER / 1024 / 1024" | bc) FREED_GB=$(echo "scale=2; $FREE_SPACE_GB_AFTER - $FREE_SPACE_GB_BEFORE" | bc) echo "" echo "=== Results ===" echo " Before: ${FREE_SPACE_GB_BEFORE}GB free" echo " After: ${FREE_SPACE_GB_AFTER}GB free" if (( $(echo "$FREED_GB > 0" | bc -l) )); then echo " ✅ Freed: ${FREED_GB}GB" else echo " ⚠️ No space freed yet (may need to wait or try other methods)" fi echo "" if (( $(echo "$FREED_GB < 15" | bc -l) )) && (( $(echo "$FREE_SPACE_GB_AFTER < 5" | bc -l) )); then echo "⚠️ Still low on space. Additional steps:" echo "" echo "1. Wait a few minutes - macOS may still be processing" echo "" echo "2. Restart your Mac - this often frees up snapshot space" echo "" echo "3. Check for large files in other locations:" echo " ./free_disk_space.sh" echo "" echo "4. Manually delete the Time Machine snapshot:" SNAP=$(tmutil listlocalsnapshots / 2>/dev/null | grep -v "Snapshots for disk" | grep -v "^$" | head -1) if [ -n "$SNAP" ]; then echo " sudo tmutil deletelocalsnapshots $SNAP" fi echo "" echo "5. Use macOS Storage Management:" echo " Apple Menu → About This Mac → Storage → Manage" echo " This tool can identify and remove large system files" echo "" fi echo ""