Introduction
Over time, Ubuntu accumulates temporary files, package caches, old kernel versions, and logs that can occupy dozens of gigabytes. This guide will help you safely clean your disk, reclaim space, and keep your system tidy. We will cover built-in utilities and proven methods that work on current Ubuntu versions (20.04, 22.04, 24.04).
Requirements
- Access to a terminal (Ctrl+Alt+T or SSH).
- Superuser privileges (sudo) for most commands.
- Recommendation: Back up important data before performing mass deletions.
Step 1: Check Current Disk Usage
Before cleaning, identify which partitions are full. Run:
df -h
Example output:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 45G 3.1G 94% /
/dev/sda2 100G 12G 88G 12% /home
Use%shows fill level. If the value is close to 100%, cleaning is urgently needed.- To analyze specific folders (e.g.,
/varor/home), use:
This will show the 20 largest subdirectories insudo du -sh /var/* | sort -rh | head -20/var.
Step 2: Clean APT Cache
APT stores downloaded packages in /var/cache/apt/archives/. They often occupy hundreds of megabytes.
Clean the entire cache:
sudo apt-get clean
Deletes all package files from the cache.
Clean only obsolete packages (without removing current ones):
sudo apt-get autoclean
Additionally: Remove automatically installed dependencies that are no longer needed:
sudo apt-get autoremove --purge
⚠️ Important:
autoremovecan remove packages you installed manually if they were installed as dependencies. Review the list before confirming.
Step 3: Remove Old Kernel Versions
Each kernel update leaves an old image (200–500 MB each). Keep at least two kernels: the current one and one fallback.
- Find the current kernel:
uname -r
Example:5.15.0-86-generic. - List installed kernels:
dpkg --list 'linux-image*' | grep ^ii - Remove old kernels (e.g.,
5.15.0-78-generic):sudo apt-get remove --purge linux-image-5.15.0-78-generic linux-headers-5.15.0-78-generic💡 Tip: Do not remove the kernel matching
uname -r. You can leave one additional old kernel for rollback. - Update the bootloader (if GRUB did not update automatically):
sudo update-grub
Step 4: Clean System Logs
Systemd stores logs in /var/log/journal/. They can grow significantly.
Clean old journals (keep last 3 days):
sudo journalctl --vacuum-time=3d
Clean by size (keep no more than 100 MB):
sudo journalctl --vacuum-size=100M
Permanent limit: Edit /etc/systemd/journald.conf:
[Journal]
SystemMaxUse=100M
Then restart the service:
sudo systemctl restart systemd-journald
Step 5: Clean Temporary Files
5.1. System temporary files (/tmp)
# Delete files in /tmp not accessed in 10 days
sudo find /tmp -type f -atime +10 -delete
# Delete empty directories (optional)
sudo find /tmp -type d -empty -delete
5.2. User application caches
- Flatpak:
flatpak uninstall --unused - Snap:
sudo snap set system refresh.retain=2(keep 2 latest versions) andsudo snap remove <old-snap> - Browsers: Clear cache via browser settings or delete folders
~/.cache/chromium/,~/.cache/mozilla/.
Step 6: Find and Delete Large Files
6.1. Using find
Find files larger than 100 MB system-wide (may take time):
sudo find / -type f -size +100M -exec ls -lh {} \; 2>/dev/null | awk '{print $5, $9}'
Review the list and manually delete unnecessary files.
6.2. Using ncdu (recommended)
Install the interactive analyzer:
sudo apt-get install ncdu
Run to analyze your home folder:
ncdu ~
Or for root (requires sudo):
sudo ncdu /
Navigation: arrows to select, d to delete file/folder, q to exit.
Verify Results
- Run
df -hagain and compareUse%values with the initial ones. - For a quick check of free space:
(Note:free -hfreeshows RAM, not disk; for disk usedf). - If space is not fully freed, check if deleted files are still held open by processes (rare but possible):
Restart the relevant processes or reboot the system.sudo lsof | grep deleted
Potential Issues
❌ "No space left on device" when running commands
- Cause: Not enough space for operation temporary files.
- Solution: Manually delete the largest file (e.g., an old log or dump) from
/var/logor your home folder. Usencduto locate it.
❌ Deleting an important file/kernel
- Cause: Manual error or
autoremoveremoved a needed package. - Solution: Restore from backup or reinstall the package (
sudo apt-get install linux-image-<current>). For kernels, you can boot into an old kernel from GRUB and restore.
❌ Access errors when cleaning /tmp or caches
- Cause: Running without sudo or files locked by processes.
- Solution: Use
sudofor system folders. For user caches, operate as your user without sudo.
❌ Cleaning did not have the expected effect
- Cause: Large files hidden elsewhere (e.g., in
/var/lib/dockerfor Docker,~/snapfor Snap). - Solution: Analyze specific subdirectories with
ncduordu. For Docker:docker system prune -a(caution: removes all unused images and containers).
❌ System fails to boot after kernel removal
- Cause: Deleted the current and only working kernel.
- Solution: Boot into Recovery Mode via GRUB and reinstall the kernel:
sudo apt-get install linux-image-generic sudo update-grub