Introduction / Why This Is Needed
Cache in Ubuntu consists of temporary files that the system and applications save to speed up operations. Over time, the cache (especially APT cache and systemd journals) can occupy gigabytes of disk space. Regular cleanup helps:
- Free up space on the system partition, which is critical for SSDs with small capacity.
- Speed up the system, as some services (like journald) stop lagging when logs become excessively large.
- Maintain order by avoiding the accumulation of unnecessary data.
This guide is suitable for Ubuntu 20.04 and newer. All commands work on a standard installation without additional software.
Requirements / Preparation
Before starting, ensure:
- You have terminal access (Ctrl+Alt+T) and superuser privileges (ability to run
sudo). - Your system is Ubuntu 20.04, 22.04, 24.04 or a derivative (Kubuntu, Xubuntu).
- You understand what you are deleting: the commands below are safe, but careless use of
rm -rfcan lead to data loss.
⚠️ Important: If you are working on a server or a production system, back up important data before performing a mass cleanup.
Step-by-Step Instructions
Step 1: Check Current Disk Usage
First, determine where exactly space is lacking. This will help assess the impact of the cleanup.
# Overall disk partition usage
df -h
# Analysis of large directories in root (may take some time)
sudo du -sh /* 2>/dev/null | sort -rh | head -20
The du command will show the top 20 largest directories. Pay attention to /var (logs, caches) and /home (user data).
Step 2: Clean the APT Cache
The APT cache stores downloaded .deb package files. After installation, they are usually no longer needed.
# Remove ALL downloaded .deb files (most aggressive option)
sudo apt clean
# Remove only obsolete files (those no longer available in repositories)
sudo apt autoclean
# Additionally: remove unnecessary dependencies (not cache, but also frees space)
sudo apt autoremove --purge
💡 Tip: If you frequently reinstall the same packages, keep the cache (do not run
apt clean). However, for most users, regular cleanup is beneficial.
Step 3: Clean systemd Journals
The systemd-journal can grow to gigabytes, especially during heavy system activity.
# Keep journals only for the last 3 days (recommended)
sudo journalctl --vacuum-time=3d
# Or limit the size of remaining journals (e.g., 100 MB)
sudo journalctl --vacuum-size=100M
# Check current journal size
sudo journalctl --disk-usage
Configure permanent limits via /etc/systemd/journald.conf (parameters SystemMaxUse, SystemKeepFree).
Step 4: Remove Temporary Files
Temporary directories /tmp and /var/tmp often contain leftovers from completed processes.
# Clean /tmp (files older than 10 days are automatically removed on reboot, but can be cleared now)
sudo rm -rf /tmp/* /var/tmp/*
# Safer option: delete only old files (older than 7 days)
sudo find /tmp -type f -atime +7 -delete
sudo find /var/tmp -type f -atime +7 -delete
⚠️ Important: Do not delete files in
/tmpif processes are currently using them (e.g., during software installation). It's best to do this during off-peak hours.
Step 5: Optional: Browser and User Data Cache Cleanup
If you still need more space, check your home directory:
# Firefox/Chrome cache (replace username with your actual username)
du -sh /home/username/.cache/
# Clear Firefox cache (close the browser first!)
rm -rf /home/username/.cache/mozilla/firefox/*.default/cache2/
# Clear Chrome/Chromium cache
rm -rf /home/username/.cache/google-chrome/Default/Cache/
rm -rf /home/username/.cache/chromium/Default/Cache/
Verify the Result
After completing the steps, check free space again:
df -h
Compare the values before and after. Typically, cleaning APT and systemd journals frees 200 MB to 2 GB of space, depending on system activity.
Possible Issues
| Issue | Solution |
|---|---|
Permission denied when running commands | Add sudo at the beginning or switch to root (sudo -i). |
| System didn't free space after cleanup | Check if files are in use by other processes (`sudo lsof |
| System became slower after journald cleanup | This is temporary: systemd rebuilds indexes. The effect should subside within a few minutes. |
Accidentally deleted an important file from /tmp | Restore from backup or restart the service that used it (often files in /tmp can simply be deleted). |
If you encounter regular disk space shortages, consider:
- Expanding the
/varpartition or adding a new disk. - Configuring automatic log rotation (logrotate for classic logs, journald for systemd).
- Moving home directories to a separate partition.
This guide covers the main sources of "junk" in Ubuntu. For more specific cases (Docker images, Flatpak/Snap cache), consult the documentation for the respective package manager.