Last active
December 17, 2025 18:17
-
-
Save apoo/24608721222618fc231dbda5e4ae79d8 to your computer and use it in GitHub Desktop.
purge-btrfs-snapshots.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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