#!/bin/bash # Fix: Free Space After Deleting Files # When you delete files but don't see space back, this script fixes it set -e echo "=== Fix: Free Space After Deleting Files ===" echo "" echo "This script handles the common issue where deleted files don't free up space" echo "because APFS snapshots are holding onto them." 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 "" # Check for Time Machine local snapshots echo "๐Ÿ“ธ Checking for Time Machine local snapshots..." TM_SNAPSHOTS=$(tmutil listlocalsnapshots / 2>/dev/null | grep -v "Snapshots for disk" | grep -v "^$") if [ -n "$TM_SNAPSHOTS" ]; then echo " Found snapshot(s) that may be holding space:" echo "$TM_SNAPSHOTS" | sed 's/^/ - /' echo "" read -p "Delete these snapshots to free space? (y/n) " -n 1 -r echo "" if [[ $REPLY =~ ^[Yy]$ ]]; then echo "" echo "๐Ÿ—‘๏ธ Deleting snapshots..." # Disable local snapshots first (helps with stuck snapshots) echo " 1. Disabling local snapshots..." sudo tmutil disablelocal 2>/dev/null || true sleep 2 # Delete each snapshot echo " 2. Deleting snapshots..." echo "$TM_SNAPSHOTS" | while read snapshot; do if [ -n "$snapshot" ]; then # Extract date part from snapshot name SNAP_DATE=$(echo "$snapshot" | sed 's/.*\.\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}-[0-9]\{6\}\)\.backup/\1/') if [ -n "$SNAP_DATE" ]; then echo " Deleting: $snapshot" sudo tmutil deletelocalsnapshots "$SNAP_DATE" 2>/dev/null && \ echo " โœ“ Deleted" || \ echo " โš ๏ธ Could not delete (may need restart)" fi fi done # Try to thin all snapshots echo " 3. Thinning all local snapshots..." sudo tmutil thinlocalsnapshots / 1000000000 4 2>/dev/null || true # Re-enable local snapshots echo " 4. Re-enabling local snapshots..." sleep 2 sudo tmutil enablelocal 2>/dev/null || true echo " โœ“ Snapshot cleanup completed" fi else echo " โœ“ No Time Machine snapshots found" fi echo "" # Force purge of memory and disk caches echo "๐Ÿงน Purging system caches..." if command -v purge &> /dev/null; then sudo purge echo " โœ“ Purge completed" else echo " โš ๏ธ Purge command not available" fi echo "" # Wait a moment for system to update echo "โณ Waiting for system to update disk space..." sleep 5 # Check new free space 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" fi echo "" if (( $(echo "$FREED_GB < 15" | bc -l) )) && (( $(echo "$FREE_SPACE_GB_AFTER < 5" | bc -l) )); then echo "โš ๏ธ If space still not freed, try these steps:" echo "" echo "1. **Restart your Mac** - This is often the most effective solution." echo " Snapshots are sometimes only released after a restart." echo "" echo "2. Check if Time Machine is trying to connect:" echo " - Open System Settings โ†’ Time Machine" echo " - If it says 'Connecting...', wait for it to finish or disconnect" echo " - Then try deleting snapshots again" echo "" echo "3. Manually delete the snapshot after disabling Time Machine:" echo " sudo tmutil disablelocal" echo " sudo tmutil deletelocalsnapshots " echo " sudo tmutil enablelocal" echo "" echo "4. 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 ""