How To Clean Up Snap Package Versions In Linux [Quick Tip]
Snap packages are not everyone’s favorite but they are an integral part of the Ubuntu ecosystem.
It has its pros and cons. One of the negatives is that Snap packages are usually bigger in size and take a lot of disk space.
This could be a problem if you are running out of disk space, specially on the root partition.
Let me share a neat trick that you could use to cut down the disk spaced used by Snap packages.
Cleaning up old Snap package versions to free disk space
The system files related to snap are stored in the /var/lib/snapd directory. Based on the number of Snap packages you have installed, this directory size could be in several GBs.
Don’t just take my word for it. Do an assesement by using the du command to check the directory size.
abhishek@its-foss:~$ sudo du -sh /var/lib/snapd 5.4G /var/lib/snapdYou may also use the Disk Usage Analyzer GUI tool to see the disk usage in Ubuntu.
That’s a lot, right? You could free up some disk space here.
By design, Snap keeps at least one older version of the packages you have installed on your system.
You can see this behavior by using the Snap command:
snap list --allYou should see the same package listed twice with different version and revision number.
To free up disk space, you can delete the additional package versions. How do you know which one to delete? You can see that these older packages are labeled ‘disabled’.
Don’t worry. You don’t have to manually do it. There is sort of automatic way to do it thanks to a nifty bash script written by Alan Pope while he was working in the Snapcraft team.
🚧Kindly note that this script may not work for French and some other languages because the label ‘disabled’ is likely to be different in different languages.I hope you know how to create and run a bash shell script. Basically, create a new file named clean-snap.sh and add the following lines to it.
#!/bin/bash # Removes old revisions of snaps # CLOSE ALL SNAPS BEFORE RUNNING THIS set -eu snap list --all | awk '/disabled/{print $1, $3}' | while read snapname revision; do snap remove "$snapname" --revision="$revision" doneSave it and close the editor.
To run this script, keep it in your home directory and then open the terminal in Ubuntu and run this command:
sudo bash clean-snap.shYou can see that it starts removing the older version of packages.
If you check the disk space used by Snap now, you’ll see that the directory size is reduced now.
abhishek@its-foss:~$ sudo du -sh /var/lib/snapd 3.9G /var/lib/snapdIf this works for you, you could run this command occasionally.
How does this script work?
If you are curious about what does this script do, let me explain.
You have already seen the output of the “snap list –all” command. It’s output is passed to the awk command. Awk is a powerful scripting tool.
The awk ‘/disabled/{print $1, $3}’ part looks for the string ‘disabled’ in each row and if it is found, it extracts the first column and third column.
This output is further passed to a combination of while and read command. Read command gets the value of first column to snapname and third column to revision variable.
These variables are then used to run the snap remove command to delete with the name of the span package name and its revision number.
The while loop runs as long as there are rows found with ‘disabled’ string in it.
This all makes sense easily if you know a little bit about shell scripting. If you are not familiar with, we have a bash tutorial series for beginners for you.
Did you get your GBs back?
You may see some forums advising to set up the Snap package retention value to 2.
sudo snap set system refresh.retain=2I don’t think it’s needed anymore. Snap’s default behavior now is to store total 2 versions for any package.
Altogether, if you are running out of space, getting rid of the additional package version could surely one of the ways to free up disk space on Ubuntu.
If this tutorial helped you free some space, let me know in the comment section.
Từ khóa » Gỡ Ubuntu
-
Install Grub Customizer On Ubuntu 22.04 & Other Versions
-
How To Disable And Remove LightDM On Linux
-
5 Things To Do After Upgrading To Ubuntu 22.04 LTS
-
How To Add, Remove And Update Software In Linux Using Apt
-
22 Things To Do After Installing Ubuntu 22.04 Jammy Jellyfish
-
[Solved] Target Packages Is Configured Multiple Times Error In Ubuntu
-
How To Uninstall Linux From Windows 11
-
How To Install A Desktop Environment/GUI In Ubuntu Server
-
How To Completely Remove A Linux Distro From WSL
-
How To Completely Uninstall Google Chrome From Ubuntu
-
How To Install Tor Browser On Ubuntu 22.04 LTS Jammy
-
How To Change Default Kernel In Ubuntu 22.04 | 20.04 LTS
-
How To Uninstall Applications From Ubuntu [Beginner's Guide]
-
3 Ways To Install Nodejs & NPM On Ubuntu 22.04 LTS Jammy