Freeing Disk Space on Ubuntu Server

PowerADM.com / Linux / Ubuntu / Freeing Disk Space on Ubuntu Server

Many temporary and unused files are left on the Ubuntu drive after installing/uninstalling packages and dependencies. In addition, log files and old versions of the kernel can take up a fair amount of disk space. This article shows you how to clean up your Ubuntu Server hard drive by deleting temporary and unused files.

Find the Largest Files and Directories in Ubuntu

To find the largest files and directories that take up a lot of space on Linux, you can use the ncdu tool:

$ sudo ncdu /

The utility will display a list of files, directories, and their sizes.

Find large files with ncdu on Ubuntu

You can also use the following commands to determine folder sizes:

$ df -hT
$ du -h --max-depth

Removing Orphaned Packages and Dependencies on Ubuntu

A lot of space on Ubuntu’s hard drive can be taken up by files left over after removing or updating packages and their dependencies with the apt package manager.

Remove unused packages from the cache:

$ sudo apt autoclean

Clear the apt cache in Ubuntu:

$ sudo apt clean
You can find out the current size of the apt cache on Ubuntu:

$ sudo du -sh /var/cache/apt

Delete unused dependencies:

$ sudo apt autoremove
It is recommended that you remove dependencies immediately after uninstalling an application in Ubuntu with the command:

$ sudo apt autoremove package_name

If you are using the snap package manager, keep in mind that when you remove a package, it will remove all the old versions except for the last three versions.

You can change Snap settings to store only one version of a package:

$ snap set system refresh.retain=2

Use this script to remove all unused snap packages:

#!/bin/bash
set -eu
LANG=en_US.UTF-8 snap list --all | awk '/disabled/{print $1, $3}' |
while read snapname revision; do
snap remove "$snapname" --revision="$revision"
done

snap remove unused packages

List removed dpkg packages:

$ dpkg -l | awk '/^rc/ {print $2}'

In order to remove orphaned package files, run the following command

$ dpkg -l | awk '/^rc/ {print $2}' | xargs dpkg --purge

or

$ aptitude purge ~c

How to Delete Old Unused Kernel Versions on Ubuntu

Old Ubuntu kernel images can take up quite a lot of space (200-400MB).

List all the available kernel images in Ubuntu:

$ apt list --installed | egrep "linux-image|linux-headers"

or

$ dpkg --list | egrep "linux-image|linux-headers"

Previous kernel versions are marked as rc.

You can print the current kernel version with the command:

$ uname -a

list installed old ubuntu kernels

You can remove the old version of the kernel with the command:

$ sudo apt purge linux-image-5.15.0-25-generic

You can use the following command to clean up old kernel versions in new versions of Ubuntu

$ sudo apt-get autoremove

Use the following script to remove all kernel versions except the current and penultimate ones:

apt-get purge $(dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | head -n -1)
apt autoremove
update-grub
update-grub2

How to Clear the Systemd Journal Logs

Ubuntu log files can also take up a lot of disk space.

Display the current size of the systemd log in Ubuntu:

$ journalctl --disk-usage

journalctl - check log disk usage

In order to delete archived journal files that are more than 2 weeks old:

$ sudo journalctl --vacuum-time=2weeks

Reduce the size of the systemd log to 200 MB:

$ sudo journalctl --vacuum-size=200M
Leave a Reply

Your email address will not be published. Required fields are marked *