How to Free Up Disk Space on Ubuntu and Linux Mint

Who doesn’t want to save disk space? If not managed wisely, the largest of disks may fill in no time and you might be looking for new disks. If you are using high priced limited disk servers such as vps or dedicated server on reputed hosting companies saving disk space may result in saving good amount of money. So why not free some space occasionally!

Lets start with checking current disk usage

df -h

Result:

ubuntu@inimisttech:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root       7.7G  5.8G  1.9G  76% /
devtmpfs        3.9G     0  3.9G   0% /dev
tmpfs           3.9G     0  3.9G   0% /dev/shm
tmpfs           795M  876K  795M   1% /run
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/loop8       68M   68M     0 100% /snap/lxd/22753
tmpfs           795M     0  795M   0% /run/user/1000
/dev/loop12      25M   25M     0 100% /snap/amazon-ssm-agent/6312
/dev/loop3       48M   48M     0 100% /snap/snapd/17336
/dev/loop1       56M   56M     0 100% /snap/core18/2620
/dev/loop10      64M   64M     0 100% /snap/core20/1695

This is a low disk testing server but results can be quite different for you. So I will jump into clearing disk space

Prerequisites

Access to server or system with root user or a user with sudo access.

Step 1: Remove APT Cache

Ubuntu keeps a cache of the installed packages which are downloaded or installed earlier even after uninstallation. You can see the cache files in this location /var/cache/apt

To check the disk space consumed you can use the following command.

sudo du -csh /var/cache/apt

Your will get an output that shows the space used by the cache folder.

Output
70M /var/cache/apt
70M total

You can clean this cache using the following command.

sudo apt-get clean

This command will clean up the entire cache to free up this space. If you wish to clean up only the outdated packages, you can use the following command.

sudo apt-get autoclean

Step 2: Clean Journal Logs

Each Linux disto has their own logging system to investigate what is going on in the system. You will have all logging data related to kernel or any other services.

Over time these logs consume more space. You can check the log size using the following command.

sudo journalctl --disk-usage

You can clear these logs which are older than certain days using the following command.

sudo journalctl --vacuum-time=2d

This command will delete all logs that are older than 2 days.

Step 3: Clean Up unused Packages

If you have packages that are not used you can use the following command to remove these packages.

sudo apt-get remove package_name

Step 4: Remove Old Kernels

You can remove the kernels that are no longer needed and that are installed from Ubuntu archive while system updates using the following command.

sudo apt-get autoremove --purge

Note: This command will not remove the kernels that are installed manually.

Step 5: Remove older versions of Snap applications

You probably already know that Snap packages are bigger in size. Actually Snap stores at least two older versions of the application, in case, you want to go back to the older version. This eats up a huge amount of space. In my case, it was over 1 GB.

du -h /var/lib/snapd/snaps
4.0K    /var/lib/snapd/snaps/partial
519M    /var/lib/snapd/snaps

A member of Snapcraft team at Canonical named Alan Pope has created a small script that you can use and run to clean all the older versions of your snap apps.

What you have to do here is to create a new shell script and use the following lines in your script:

#!/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"
    done

Give it execute permission, run the shell script with sudo and see the magic. The script removed the older Snap packages and freed over half of the 519 MB space used by Snap.

du -h /var/lib/snapd/snaps
4.0K	/var/lib/snapd/snaps/partial
260M	/var/lib/snapd/snaps

Conclusion

Now you have learned how to clean up your Ubuntu server or system by cleaning the journal logs, cache, unused packages, old kernels, snaps.

Thanks for your time. If you have any feedback please leave a comment below.

Leave a Reply