Skip to content

Instantly share code, notes, and snippets.

@apoo
Last active December 17, 2025 18:17
Show Gist options
  • Select an option

  • Save apoo/24608721222618fc231dbda5e4ae79d8 to your computer and use it in GitHub Desktop.

Select an option

Save apoo/24608721222618fc231dbda5e4ae79d8 to your computer and use it in GitHub Desktop.
purge-btrfs-snapshots.sh
#!/usr/bin/env bash
set -euo pipefail
echo "🚨 Purging ALL Btrfs snapshots (root + home)"
echo "--------------------------------------------"
# Sanity check
if [[ "$(findmnt -n -o FSTYPE /)" != "btrfs" ]]; then
echo "❌ Root filesystem is not Btrfs. Aborting."
exit 1
fi
echo "βœ” Btrfs detected"
echo
echo "πŸ” Locating snapshot subvolumes..."
SNAPSHOTS=$(sudo btrfs subvolume list / | awk '{print $NF}' | grep -E '(^|/)\\.snapshots/.*/snapshot$' || true)
if [[ -z "$SNAPSHOTS" ]]; then
echo "βœ… No snapshot subvolumes found."
else
echo "$SNAPSHOTS"
echo
echo "🧨 Deleting snapshot subvolumes..."
while read -r sv; do
echo "Deleting subvolume: /$sv"
sudo btrfs subvolume delete "/$sv" || true
done <<< "$SNAPSHOTS"
fi
echo
echo "🧹 Removing leftover snapshot directories..."
sudo rm -rf /.snapshots /home/.snapshots || true
echo
echo "πŸ”„ Syncing filesystem..."
sudo btrfs filesystem sync /
echo
echo "πŸ“¦ Rebalancing (reclaiming space)..."
sudo btrfs filesystem balance start -dusage=50 /
echo
echo "πŸ“Š Final disk usage:"
sudo btrfs filesystem usage /
echo
echo "βœ… Snapshot purge COMPLETE."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment