Script 05 · Package Cleanup

5. Purge Snapd

Aggressively removes Snap packages, snapd, leftover Snap data, and blocks snapd reinstallation through APT preferences.

Category: Package Cleanup Risk: Destructive Lines: calculating Language: Bash / Linux
Back to index

What this script does

  • Free disk and loop device usage.
  • Remove Snap dependency from lean VPS images.
  • Prevent snapd from returning through package dependencies.

Prerequisites

  • Root access
  • Confirmed no production service depends on Snap
  • Alternative packages available for removed apps

Execution flow

  1. Removes known snap packages
  2. Loops through remaining snaps
  3. Purges snapd
  4. Deletes leftover directories
  5. Pins snapd priority to -1

Validation checklist

  • snap list should fail or be empty
  • dpkg -l | grep snapd
  • cat /etc/apt/preferences.d/nosnap.pref

Operational cautions

  • This is destructive and can remove applications installed via Snap.
  • Ubuntu desktop components may depend on Snap on non-server systems.

Original script notes

ℹ️ Script Info: Completely uninstalls the Snap package manager and all associated applications to free up system resources, loop devices, and storage space.

⚠️ Destructive Action: This script will aggressively and permanently remove all Snap applications and the snapd daemon from your system. Ensure you do not have server dependencies running on Snap before executing.

Script source
cat << 'EOF' > purge_snap_auto.sh && chmod +x purge_snap_auto.sh && ./purge_snap_auto.sh
#!/bin/bash
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; NC='\033[0m'

echo -e "${RED}[!!!] WARNING: This script will TOTALLY remove all SNAP applications and the SNAPD system.${NC}"
echo -e "${YELLOW}[*] Press CTRL+C within 5 seconds to cancel...${NC}"
sleep 5

echo -e "\n${GREEN}[1/5] Removing specific Snap packages...${NC}"
# Safe remove function
safe_remove() { snap list | grep -q "^$1" && sudo snap remove --purge "$1" || echo "$1 not installed, skipping."; }

safe_remove "firefox"
safe_remove "snap-store"
safe_remove "gnome-3-38-2004"
safe_remove "gtk-common-themes"
safe_remove "snapd-desktop-integration"
safe_remove "lxd"

echo -e "\n${GREEN}[2/5] Cleaning up remaining snaps...${NC}"
# Aggressive loop to remove leftovers before core
while [ "$(snap list 2>/dev/null | wc -l)" -gt 1 ]; do
    for snap in $(snap list | awk 'NR>1 {print $1}'); do
        echo "Removing: $snap..."
        sudo snap remove --purge "$snap" || true
    done
done

# Remove final base
safe_remove "core20"
safe_remove "bare"
safe_remove "core"

echo -e "\n${GREEN}[3/5] Uninstalling Snapd Daemon...${NC}"
sudo apt autoremove --purge snapd -y

echo -e "\n${GREEN}[4/5] Removing leftover files...${NC}"
rm -rf ~/snap
sudo rm -rf /var/cache/snapd /var/lib/snapd /root/snap
echo "Trash files cleaned."

echo -e "\n${GREEN}[5/5] Blocking Snapd via APT Preference...${NC}"
echo -e 'Package: snapd\nPin: release a=*\nPin-Priority: -1' | sudo tee /etc/apt/preferences.d/nosnap.pref
sudo apt update

echo -e "\n${GREEN}[DONE] Snapd exterminated. Script saved at $(pwd)/purge_snap_auto.sh${NC}"
EOF
Done