Files
nas-tools/force_delete_snapshot.sh
T

132 lines
4.0 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# Force Delete Stuck Snapshot - Last Resort Methods
# When normal deletion methods fail
set -e
echo "=== Force Delete Stuck Snapshot ==="
echo ""
echo "This script tries aggressive methods to free stuck snapshot space."
echo ""
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 ""
SNAPSHOTS=$(tmutil listlocalsnapshots / 2>/dev/null | grep -v "Snapshots for disk" | grep -v "^$")
if [ -z "$SNAPSHOTS" ]; then
echo "✅ No snapshots found!"
exit 0
fi
echo "Found stuck snapshot(s):"
echo "$SNAPSHOTS" | sed 's/^/ - /'
echo ""
read -p "Try aggressive cleanup methods? (y/n) " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 0
fi
echo ""
echo "Method 1: Aggressive snapshot thinning..."
sudo tmutil thinlocalsnapshots / 1000000000 1 2>&1 || true
echo " ✓ Completed"
echo ""
echo "Method 2: System purge..."
if command -v purge &> /dev/null; then
sudo purge
echo " ✓ Purged"
else
echo " ⚠️ Purge command not available"
fi
echo ""
echo "Method 3: Checking APFS snapshots..."
APFS_SNAPS=$(diskutil apfs listSnapshots / 2>/dev/null)
if echo "$APFS_SNAPS" | grep -qi "timemachine"; then
echo " Found Time Machine snapshot in APFS"
# Try to get UUID and delete
SNAP_UUID=$(echo "$APFS_SNAPS" | grep -A 2 -i "timemachine" | grep "UUID" | awk '{print $NF}' | head -1)
if [ -n "$SNAP_UUID" ]; then
echo " Attempting to delete APFS snapshot: $SNAP_UUID"
sudo diskutil apfs deleteSnapshot / -uuid "$SNAP_UUID" 2>&1 || echo " ⚠️ Could not delete (may be protected)"
fi
else
echo " ️ Snapshot not in APFS list (ghost entry)"
fi
echo ""
echo "Method 4: Force filesystem sync..."
sync
echo " ✓ Filesystem synced"
echo ""
echo "Method 5: Check for Time Machine temp files..."
TM_TEMP=$(sudo find /System/Volumes/Data/.timemachine -type f 2>/dev/null | wc -l | tr -d ' ')
if [ "$TM_TEMP" -gt 0 ]; then
TEMP_SIZE=$(sudo du -sh /System/Volumes/Data/.timemachine 2>/dev/null | awk '{print $1}' || echo "0")
echo " Found Time Machine temp files: $TEMP_SIZE"
read -p " Delete Time Machine temp files? (y/n) " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
sudo rm -rf /System/Volumes/Data/.timemachine/* 2>/dev/null || true
echo " ✓ Cleaned"
fi
else
echo " ✓ No temp files found"
fi
echo ""
echo "⏳ Waiting for system to update..."
sleep 5
# Check results
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)
REMAINING_SNAPS=$(tmutil listlocalsnapshots / 2>/dev/null | grep -v "Snapshots for disk" | grep -v "^$" | wc -l | tr -d ' ')
echo ""
echo "=== Results ==="
echo " Free space: ${FREE_SPACE_GB_BEFORE}GB → ${FREE_SPACE_GB_AFTER}GB"
if (( $(echo "$FREED_GB > 15" | bc -l) )); then
echo " ✅ Successfully freed: ${FREED_GB}GB"
elif (( $(echo "$FREED_GB > 0" | bc -l) )); then
echo " ✅ Freed: ${FREED_GB}GB"
else
echo " ⚠️ No space freed yet"
fi
if [ "$REMAINING_SNAPS" -gt 0 ]; then
echo " ⚠️ Snapshot still listed (ghost entry)"
echo ""
echo " The snapshot entry may persist, but if space was freed,"
echo " it's likely just a database entry and not taking space."
else
echo " ✅ Snapshot removed!"
fi
echo ""
if (( $(echo "$FREED_GB < 15" | bc -l) )); then
echo "⚠️ Space still not fully freed. Final options:"
echo ""
echo "1. **Restart your Mac** - This is the most reliable solution."
echo " The snapshot will be released after restart."
echo ""
echo "2. Wait 10-15 minutes - macOS may still be processing"
echo ""
echo "3. Check if space is actually available but not showing:"
echo " - Try creating a large file to test"
echo " - Check Activity Monitor → Disk tab"
echo ""
fi
echo ""