This script finds and removes orphaned snap packages on a Linux system, unless they serve as a default provider for another snap package.
- Save this script as a file (e.g.
remove_orphaned_snaps.sh
) - Make the script executable with
chmod +x remove_orphaned_snaps.sh
- Run the script with
./remove_orphaned_snaps.sh
- The script uses
snap connections
to find all snap packages that have no connections (i.e. are orphaned). - It filters out themes and slot-based connections.
- It extracts the package names from the output and stores them in the
orphan
variable. - It checks if any of the orphaned packages are default providers for other packages by parsing the
snap.yaml
files. - If an orphaned package is a default provider, it will not be removed.
- Otherwise, the script will remove the orphaned package using
sudo snap remove
.
- Be careful when running this script, as it will permanently remove packages without prompting for confirmation.