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.
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
$ sudo du -sh /var/cache/apt
Delete unused dependencies:
$ sudo apt autoremove
$ 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
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.
$ uname -a
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
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