GICT knowledge base
Best Practices for Maintaining Free Disk Space in Ubuntu Systems
Published
Introduction
One of the most common issues that Ubuntu users face over time is running out of disk space. At first, it might not seem like a big problem. But once your storage gets full, updates fail, applications crash, and in worst cases, your system may stop booting.
Disk space shortages happen for many reasons—unused packages, old kernels, log files, or simply storing too many large files without realizing it. While emergency cleanup can temporarily free space, it’s much better to adopt best practices that help prevent the issue altogether.
In this article, we’ll explore practical strategies to keep your Ubuntu system clean, optimized, and running smoothly. These tips work for both desktops and servers, and they’ll help you avoid stressful situations where every gigabyte counts.
Prerequisites
Before following along, make sure:
- You are running Ubuntu (desktop or server).
- You have sudo privileges.
- You are comfortable entering commands into the terminal.
- You understand the basics of file paths and system directories.
1. Monitor Disk Usage Regularly
The key to prevention is awareness. If you know which directories and files consume the most space, you can take action before it becomes critical.
Check overall disk usage:
df -h
This shows partitions, used space, and available space in a human-readable format.
Find large directories at the root level:
sudo du -h --max-depth=1 / | sort -hr | head -n 20
Explore directories interactively with ncdu:
sudo apt install ncdu -y
sudo ncdu /
ncdu is a great tool for visually exploring disk usage and deleting unnecessary files. By making it a habit to check storage weekly, you can spot growth trends early.
2. Keep Package Cache Clean
Whenever you install or update software, Ubuntu stores .deb package files in /var/cache/apt/archives/. Over time, this cache grows large.
Clean all cached packages:
sudo apt clean
Remove outdated package files:
sudo apt autoclean
Uninstall unused dependencies:
sudo apt autoremove
✅ Best practice: Run these commands once every week or two, especially if you install many packages.
3. Manage Old Kernels
Ubuntu doesn’t automatically remove older kernel versions after updates. These can consume several gigabytes.
List installed kernels:
dpkg --list | grep linux-image
Check your active kernel:
uname -r
Remove old versions (but keep the active one):
sudo apt remove --purge linux-image-x.x.x-x-generic
Update GRUB:
sudo update-grub
⚠️ Be careful not to remove the kernel currently in use. Always double-check with uname -r.
4. Control Log File Growth
Log files are useful for troubleshooting but can grow out of control, especially on servers with heavy traffic.
Check log file sizes:
sudo du -sh /var/log/*
Truncate a log without deleting it:
sudo truncate -s 0 /var/log/filename.log
Automate log rotation with logrotate: Ubuntu comes with logrotate, which compresses, rotates, and deletes old logs automatically. You can customize its behavior in /etc/logrotate.conf.
5. Manage Snap Packages
Snap applications are convenient but often take up a lot of space because multiple versions are stored.
List installed Snap packages:
snap list
Remove unused Snaps:
sudo snap remove package-name
Reduce retained versions to 2:
sudo snap set system refresh.retain=2
Remove old revisions:
sudo snap remove --purge $(snap list --all | awk '/disabled/{print $1, $2}')
This step alone can free several gigabytes.
6. Find and Remove Large Files
Sometimes, personal files like videos, ISO images, or old backups consume most of the space.
Search for files larger than 500 MB:
sudo find / -type f -size +500M
Delete unnecessary files:
sudo rm /path/to/file
Alternatively, move them to an external drive or cloud storage to save space.
7. Use Separate Partitions for Stability
On systems where uptime and reliability matter, having a separate /home, /var, or /tmp partition is a best practice.
For example:
- /home partition prevents user files from filling up system space.
- /var partition isolates logs and caches.
- /tmp partition prevents temporary files from crashing the system.
Setting this up during installation or using tools like gparted can significantly improve system stability.
8. Automate Cleanup Tasks
Instead of cleaning manually, automate the process.
Open your crontab:
crontab -e
Add a weekly cleanup job:
0 3 * * 0 sudo apt autoremove -y && sudo apt autoclean -y
This runs every Sunday at 3 AM, keeping your system clean automatically.
9. Use Disk Quotas for Multi-User Systems
On shared servers, one careless user can fill up the entire disk. Setting up disk quotas prevents this.
Install quota tools:
sudo apt install quota
Enable quotas on partitions in /etc/fstab.
Assign limits per user with:
sudo edquota username
This ensures fair usage and prevents system-wide issues.
Conclusion
Keeping free disk space available in Ubuntu is not just about emergency fixes—it’s about long-term maintenance and discipline. By monitoring disk usage, regularly cleaning caches, managing logs, pruning old kernels, and handling Snap packages properly, you can prevent your system from slowing down or breaking.
For power users and servers, adding automation, quotas, and dedicated partitions ensures that your system stays reliable even under heavy use.
Adopting these best practices means you’ll spend less time fighting storage issues and more time enjoying a stable, responsive Ubuntu system.
Related guides
How to Find Services That Need a Restart After Updates with needrestart
Find services using outdated libraries with needrestart on Ubuntu. Read restart reports, review automatic restart policy and plan maintenance safely.
UbuntuHow to Enable Brotli Compression in Nginx on Ubuntu 24.04
Enable Brotli in Ubuntu Nginx, preserve gzip fallback, verify response headers and avoid stale precompressed assets. Measure savings on your own files.
UbuntuHow to Prevent Overlapping Cron Jobs with flock
Prevent overlapping cron jobs with flock. Use a shared lock, understand skipped runs and verify that every entry point follows the locking policy.
UbuntuSecuring Ubuntu 24 SSH and other best practices
How to Lock Down Your Ubuntu 24 Server: A Beginner's Guide So, you just spun up a brand new Ubuntu 24 server. You may find a lot of SSH brute force logins failures.
