Script to remove or reinstall snap Ubuntu 26.04 / 24.04

#!/bin/bash

### GENERAL
##
#
# bi-indexed record fields
declare -i -r name=0 ver=1 rev=2 trk=3 pub=4 notes=5
declare -a -r lookup=( name   ver   rev   trk   pub   notes )

clear_cache()		{ sudo rm -rf /var/cache/snapd/ ;}
uninstall_snapd()	{ sudo apt-get purge snapd && sudo apt autoremove ;}
cleanup_home()		{ rm -rf "$HOME/snap" ;}
continue_or_exit()	{ read -s -n1 -p"Continue ? [Y/n]" ; [ "${REPLY^^}" = "N" ] && exit ;}

### FUNCTIONS
##
#
uninstall_where()
{
	local -i fieldnr="$1" ; local -a line

	echo -e "\n# removing snaps where ${lookup[fieldnr]} $2 $3"
	snap list | tail -n +2 | while read -a line
	do
		if [ "${line[fieldnr]}" "$2" "$3" ]
		then sudo snap remove --purge "${line[name]}" || continue_or_exit
 		fi
		wait
	done
}
uninstall_all_snaps()
{
	uninstall_where "$pub"  != 'canonical**'
	uninstall_where "$name"  = 'snap-store'
	uninstall_where "$notes" = '-'
	uninstall_where "$notes" = 'base'
	uninstall_where "$name"  = 'snapd'
}
pin_snapd()
{
	cat<<-BLOCK_IN_APT | sudo tee /etc/apt/preferences.d/nosnap.pref
	Package: snapd
	Pin: release a=*
	Pin-Priority: -10
	BLOCK_IN_APT
}
disable_snapd()
{
	sudo systemctl disable snapd.socket
	sudo systemctl stop snapd.service
	sudo systemctl disable snapd.service
	sudo systemctl mask snapd.service
}
enable_snapd()
{
	sudo systemctl unmask snapd.service
	sudo systemctl enable snapd.socket
	sudo systemctl enable snapd.service
	sudo systemctl start  snapd.service
}
reinstall()
{
	sudo rm /etc/apt/preferences.d/nosnap.pref >/dev/null
	sudo apt-get update && sudo apt-get upgrade
	sudo apt-get install snapd
	enable_snapd
	sudo snap install snap-store
}

### MAIN
##
#
declare -u choice
while :
do
	clear
	cat<<-MENU
	DESNAPIFYER
	___________________________________
	snap list:

	$( ps -A |grep snapd >/dev/null && snap list )
	___________________________________
	(basic) snap remove steps:

	[1] Remove installed snaps
	[2] Remove snapd
	[3] Pin snapd
	[4] Remove snap directory from home
	___________________________________
	(advanced) special tools:

	[5] disable snapd in systemd
	[6] enable  snapd in systemd
	___________________________________

	[R] regret: install snap again
	[Q] quit
	___________________________________
MENU
	read -r -s -n1 choice
	case "$choice" in
	1) uninstall_all_snaps			; continue_or_exit	;;
	2) clear_cache && uninstall_snapd	; continue_or_exit	;;
	3) pin_snapd					; continue_or_exit	;;
	4) cleanup_home					; continue_or_exit	;;
	5) disable_snapd				; continue_or_exit	;;
	6) enable_snapd					; continue_or_exit	;;
	R) reinstall					; continue_or_exit	;;
	Q) exit												;;
	esac
done

This is done on a ‘best effort’ base without guarantees.
Please let me know of any bugs you encounter.

7 Likes

@tkn I believe you mentioned this was tested on your system with 24.04 LTS and 26.04 LTS.

3 Likes

Yes I did :slight_smile:
But I always stay at the careful side.

Life have thrown at me the most weird and unexpected stuff. One can never be careful enough. :grinning_face_with_smiling_eyes:

If it works for me then it should work for you ofcourse.

5 Likes

Yes thats true, thanks for sharing.

3 Likes