Introduction / Why This Is Needed
Occupied disk space is one of the most common reasons for Ubuntu slowdowns and errors like "No space left on device". This guide will show you how to not just delete files randomly, but to precisely locate the "space hogs" and clean them up safely. You'll gain full control over your disk using the terminal.
Requirements / Preparation
- Ubuntu 20.04 or newer (the instructions also apply to other Debian-based distributions).
- Access to the terminal (
Ctrl+Alt+T). - sudo privileges to clean system directories and install utilities.
- Basic familiarity with the command line.
Step 1: Install Necessary Utilities
Modern Ubuntu versions already include everything needed for basic analysis (df, du), but for convenience, let's install two powerful tools:
ncdu— an interactive text-based analyzer (similar todu, but with navigation and sorting).duf— a beautiful and informative alternative todf(shows usage by partition, Inodes, filesystem types).
# Update the package list
sudo apt update
# Install ncdu and duf
sudo apt install -y ncdu duf
What the command does: apt install downloads and installs programs from official repositories. The -y flag automatically confirms the installation.
Step 2: Check Overall Disk Usage
First, let's see which partition is full.
# Run duf — this is the main command for a quick overview
duf
You will see a table where:
Size— the total partition size.Used— used space.Avail— available space.Use%— the fill percentage (this is what we're looking at!).
Alternative: the old reliable df -h (human-readable).
df -h
Important: Note the mount point (Mounted on) of the full partition. Usually it's / (root partition) or /home.
Step 3: Find the Largest Directories
Now we search for what specifically is taking up space on the problematic partition. For example, if / is full:
# Analyze the root partition. This might take a while!
sudo ncdu /
How to use ncdu:
- After launching, you'll see a list of folders sorted by descending size.
- Up/Down arrows — navigation.
- Enter — enter a folder (analyze its contents).
- d — delete the selected file/folder (be careful!).
- q — quit.
Tip: Start by analyzing /home (your personal files) and /var (logs, cache). Most often, the "culprits" are:
- Browser caches (
~/.cache/*). - Junk in
~/Downloads. - Old package versions or kernels (
/var/cache/apt/,/boot). - Shared BitTorrent clients (
/home/user/Downloads).
💡 Tip: If
ncdudoesn't find anomalies, check hidden folders (starting with.) in your home directory. They are visible inncdu.
Step 4: Clean the APT Package Manager Cache
APT stores downloaded .deb packages in /var/cache/apt/archives. They can be safely removed after installing programs.
# Clean the APT cache (frees up to hundreds of MB)
sudo apt-get clean
# Or a gentler option — remove only obsolete packages
sudo apt-get autoclean
Difference: clean removes all cached packages. autoclean removes only those that can no longer be downloaded (obsolete).
Step 5: Remove Old Logs and Temporary Files
System logs can grow large. journalctl manages systemd logs.
# Clear logs older than 7 days (safe)
sudo journalctl --vacuum-time=7d
# Or clear if logs take up more than 500 MB
sudo journalctl --vacuum-size=500M
Temporary files: Cleaning /tmp is usually safe, but some programs may use it for current data.
# Delete contents of /tmp (not the directory itself!)
sudo rm -rf /tmp/*
⚠️ Important: Do not manually delete files in
/var/logdirectly (rm /var/log/*.log). Usejournalctlor logrotate (configured by default). Deleting "by hand" can break running processes that write to these files.
Step 6: Check the Result
After each cleanup, return to Step 2 and see if the picture has changed.
duf
If space hasn't been freed, go back to ncdu and look for other large directories. The issue might be in user data (videos, iso images, virtual machines).
Potential Issues
| Problem | Solution |
|---|---|
Permission denied when running ncdu / | Use sudo ncdu / to analyze system directories. Be careful with deletion! |
ncdu shows little space, but df says otherwise | Files may have been deleted, but a process still holds open file descriptors. Reboot the system or find the process via `lsof |
| APT cache cleanup doesn't free space | Check if unattended-upgrades is running in the background — it may temporarily hold the cache. |
duf won't install (package not found) | In very old Ubuntu versions, use sudo apt install dfc (alternative) or install duf via snap: sudo snap install duf. |
No space left on device error when trying to clean | Yes, irony. Free up space manually by deleting a couple of large files (e.g., an old iso), even if it's a temporary solution. |
Additional Methods (If the Main Ones Didn't Help)
1. Find Large Files Older Than N Days
Locate and remove giant artifacts (e.g., old backups):
# Find files larger than 100MB older than 30 days in /home
find /home -type f -size +100M -mtime +30 -exec ls -lh {} \;
Deletion (only after checking!):
find /home -type f -size +100M -mtime +30 -delete
2. Analyze Inodes (if df shows free space but you can't write)
df -i
If IUse% is close to 100%, the problem is the number of files, not their size. Often caused by millions of small files in a cache (e.g., npm, docker). Clean the respective caches.
3. Remove Old Kernels (caution!)
# List installed kernels
dpkg --list | grep linux-image
# Remove old ones (except the current one! Identify current via `uname -r`)
sudo apt-get remove linux-image-5.4.0-XX-generic
Never remove the last/current kernel!
Verification
- Run
duf— theUse%percentage should decrease. - Try creating a large test file (
fallocate -l 1G testfile) to confirm space is actually free. - Restart services that failed due to disk shortage (e.g.,
sudo systemctl restart postgresql) and check their logs.
Final Recommendations
- Regularity: Once a month, run
ncdu /homeandsudo ncdu /var. - Monitoring: Install
dufand run it when you suspect problems. - Automation: Add a cron job (
crontab -e) to clean the APT cache and journals, but only if you are sure it's safe. - Backups: Before mass deletion (especially via
find ... -delete), ensure important data won't be affected.