Running out of disk space on an Ubuntu Linux system can cause severe performance issues, prevent critical security updates from installing, and even stop your graphical desktop environment from loading.
While Ubuntu includes graphical disk usage analysers, the most effective and thorough way to reclaim storage space is by using the terminal.
However, running aggressive cleanup commands without understanding them can accidentally break your system. This guide explains how to safely remove gigabytes of temporary files, old package caches, and unnecessary system logs without risking your core operating system.
Step 1: Clean the APT Package Cache
Whenever you install or update software using the apt package manager, Ubuntu downloads the installation files (known as .deb files) and stores them in a local cache. Over time, this cache can grow to several gigabytes, filled with outdated versions of software you no longer need.
You can safely delete these cached files without affecting your currently installed applications.
To clean the cache, open your terminal (Ctrl + Alt + T) and run the following command:
sudo apt-get clean
This command empties the /var/cache/apt/archives/ directory completely. It is entirely safe to run, as Ubuntu will simply re-download any required files the next time you attempt an installation.
Alternatively, if you want to be slightly more conservative, you can run:
sudo apt-get autoclean
This command only removes installation files for software versions that are no longer available in the official repositories, keeping the cache for newer software intact.
Step 2: Remove Unused Dependencies
When you install an application, Ubuntu automatically installs supporting software called “dependencies” required for that application to run. If you later uninstall the main application, these dependencies are often left behind, taking up valuable disk space.
To safely identify and remove these orphaned packages, use the autoremove command:
sudo apt-get autoremove
Ubuntu will calculate how much space will be freed and present you with a list of packages that are no longer needed. Press Y and hit Enter to confirm their removal. This is one of the most effective ways to reclaim space on an older Ubuntu installation.
Step 3: Clean the Systemd Journal Logs
Ubuntu uses a logging service called systemd-journald to record system events, errors, and application activity. By default, these logs can consume up to 10% of your total file system, which can translate to several gigabytes of text files that average users will never read.
You can safely truncate these logs without damaging your system.
First, check how much space your journals are currently using by running:
journalctl --disk-usage
To clear out older logs and reclaim that space, you can instruct Ubuntu to delete any logs older than a specific timeframe. For example, to delete all logs older than 3 days, run:
sudo journalctl --vacuum-time=3d
Alternatively, you can restrict the logs to a specific maximum size, such as 100 megabytes:
sudo journalctl --vacuum-size=100M
Step 4: Remove Old Snap Revisions
If you use Ubuntu, you are likely using Snap packages. By design, the Snap daemon (snapd) keeps older versions of applications installed when an update occurs, allowing you to roll back if a new update breaks. However, keeping multiple versions of heavy applications like web browsers or communication tools uses a massive amount of disk space.
You can force Ubuntu to remove these older revisions.
Create a small cleanup script by running the following command to open the nano text editor:
nano clean-snaps.sh
Paste the following code into the editor:
#!/bin/bash
set -eu
snap list --all | awk '/disabled/{print $1, $3}' |
while read snapname revision; do
snap remove "$snapname" --revision="$revision"
done
Save the file by pressing Ctrl + O, then Enter, and exit using Ctrl + X.
Make the script executable by running:
chmod +x clean-snaps.sh
Finally, run the script with administrator privileges to remove the disabled snap revisions:
sudo ./clean-snaps.sh
Step 5: Empty the User Trash
It sounds obvious, but files deleted via the Ubuntu graphical file manager are moved to the Trash folder rather than being permanently deleted. If you primarily work in the terminal, you may forget to empty the graphical trash bin.
You can empty the trash for your specific user account directly from the terminal by removing the contents of the local trash directory:
rm -rf ~/.local/share/Trash/*
By regularly running these five steps, you can prevent your Ubuntu system from suffering performance degradation due to a lack of storage capacity.