Introduction / Why This Is Needed
Over time, Ubuntu accumulates temporary files, package caches, old logs, and unnecessary dependencies that can occupy anywhere from several hundred megabytes to several gigabytes. This not only reduces free disk space but can also slow down the system. In this guide, you will learn how to safely and effectively clean your disk, reclaiming precious space without risking system stability.
Requirements / Preparation
Before you begin, ensure that:
- You have Ubuntu 20.04 or newer installed.
- You have access to a terminal (Ctrl+Alt+T).
- Some commands require administrator privileges (sudo).
- You understand that deleting certain files may be irreversible. It is recommended to back up important data.
Step-by-Step Instructions
Step 1: Clean APT Cache
APT (Advanced Package Tool) stores downloaded deb packages in /var/cache/apt/archives. These files are usually not needed after program installation.
sudo apt clean
This command will delete the entire package cache. If you want to keep already installed packages (for example, for offline installation), use sudo apt autoclean—it will remove only obsolete files.
Step 2: Remove Old Dependencies
When installing programs via APT, dependencies are installed automatically. After removing a program, its dependencies often remain. The autoremove command finds and removes them.
sudo apt autoremove
For a deeper cleanup (removing configuration files as well), add the --purge flag:
sudo apt autoremove --purge
⚠️ Important:
autoremovewill not remove packages installed manually. However, before deletion, the system will show a list—review it carefully.
Step 3: Clean systemd Journals
Systemd stores logs in binary format in /var/log/journal. They can grow indefinitely. Clean old entries, keeping, for example, only the last 3 days:
sudo journalctl --vacuum-time=3d
Or clean by size (leave no more than 100 MB):
sudo journalctl --vacuum-size=100M
To see the current size of the journals, run:
sudo journalctl --disk-usage
Step 4: Delete Temporary Files
Temporary files in /tmp and user cache (~/.cache) often contain leftovers from programs that can be safely removed.
System temporary files (requires sudo):
sudo rm -rf /tmp/*
Current user's cache (no sudo needed):
rm -rf ~/.cache/*
💡 Tip: Some applications (like browsers) may rebuild their cache, but this won't harm their operation. If you want to be more cautious, delete only subdirectories you are confident are temporary.
Step 5: Empty the Trash
Files in the Trash (~/.local/share/Trash) also occupy space. Empty it:
rm -rf ~/.local/share/Trash/*
Or via the graphical interface: open "Trash" → "Empty Trash".
Step 6: Analyze Disk Space with ncdu
To understand which directories occupy the most space, install the ncdu utility:
sudo apt install ncdu
Start an analysis of the root filesystem:
sudo ncdu /
The interface allows navigation (arrow keys) to explore disk usage. This helps find "heavy" folders not covered by previous steps (e.g., ~/Downloads or ~/VirtualBox VMs).
Step 7: Clean Old Kernels (Optional)
Old Linux kernels can occupy 200–500 MB each. Remove unnecessary ones, keeping the current and one backup kernel.
First, check installed kernels:
dpkg -l 'linux-image*' | grep ^ii
Remove a specific old kernel (e.g., linux-image-5.4.0-XX-generic):
sudo apt remove linux-image-5.4.0-XX-generic
The safe way is to use autoremove again, which will remove old kernels if they are no longer needed:
sudo apt autoremove --purge
⚠️ Important: Do not remove the kernel your current boot depends on. Find your current kernel with
uname -r.
Step 8: Clean Browser Caches (Manually)
Browser caches are stored in your home directory. For Firefox:
rm -rf ~/.cache/mozilla/firefox/*
For Chrome/Chromium:
rm -rf ~/.cache/google-chrome/Default/Cache
rm -rf ~/.cache/chromium/Default/Cache
💡 Tip: Deleting browser caches will force them to reload frequently visited sites, which may temporarily slow things down.
Verify the Result
After performing the steps, check free space:
df -h
This command shows the size and percentage of used space for each partition. For a more detailed analysis, use ncdu (see Step 6) or check specific directories:
sudo du -sh /var/cache/apt # size of APT cache
sudo du -sh /var/log # size of logs
Possible Issues
- "Permission denied" error: use
sudofor system directories (/tmp,/var/cache). For user files (~/.cache),sudois not needed. - Deleting important files: do not manually delete files in
/var/cacheor/var/logwithout understanding their purpose. Stick to the specified commands. - System fails to boot after kernel removal: ensure you left at least one working kernel. At boot, in the GRUB menu, select "Advanced options" → an old kernel if the current one was deleted.
- Cache reappears after reboot: some applications (like Flatpak) store cache in their own directories. To clean these, use the application's own commands or delete folders in
~/.local/share/flatpak/app/.
If the problem persists, check if another partition is full (e.g., with virtual machines or media files).