When i have Windows on my PC, from time to time, because of my system becomes laggy, i go into drives and folders to find unused files and get rid of them and do some other stuffs to make more available space and improve my PC for a while but when i migrate to Linux, as there is no lag or crashes (at least like Windows), I forgot to remove unused data from my PC anymore, But let’s be real: over time, even the best systems accumulate digital clutter. We install software, forget about it, update our kernels, and suddenly, that smooth performance starts to feel a little sluggish, and your disk space notification pops up, demanding attention.
I was reading an article in Linux Magazine issue 297 about this problem, i found it useful but i think there are some lack of information there, so i decided to write a blog post about it and cover as muchas i can about this.
1. Finding The Storage Leech
Before deleting anything, you need to know where the junk is hiding. Running blind with the rm command is a recipe for disaster (trust me, you don't want to accidentally delete a folder a critical program needs!).
Meet Your New Best Friend: Ncdu
The best tool for this job isn't some complex script; it's ncdu (NCurses Disk Usage). It’s super user-friendly and gives you an interactive, real-time map of your disk space right in your terminal. They define theirselfs like this :
Ncdu is a disk usage analyzer with a text-mode user interface. It is designed to find space hogs on a remote server where you don’t have an entire graphical setup available, but it is a useful tool even on regular desktop systems. Ncdu aims to be fast, simple, easy to use, and should be able to run on any POSIX-like system.
To start hunting from your system's root directory (/), you'll need to install it first if you don't have it:
sudo apt install ncdu
sudo ncdu /
Once running, you can navigate folders like you're browsing files, instantly seeing which directories (like /var/log or a big ~/Downloads folder) are the heaviest.
Manual Hunting for Mega Files
If you prefer a direct listing of massive files (over 50MB) without the interactive interface, the find command is the best tool. This is particularly useful when accessing a server via SSH.
sudo find / -size +50M -type f -exec du -h {} \; | sort -n
2. APT : For Packages and Dependencies
The safest place to start cleaning is always with your package manager, APT, because it actually understands the structure of your system.
Removing Orphaned Dependencies
When you uninstall an app, sometimes its dependencies (packages it needed for installation) get left behind. These are called orphaned packages.
The autoremove command is brilliant because it finds these packages and safely removes them, making this command your first and most essential step for regular maintenance,.
sudo apt autoremove
Clearing Out the Downloaded Junk
Every time you install something, APT downloads the .deb file and put it away in a cache (/var/cache/apt/archives/). While handy for offline installs, this cache often just sits there taking up space for ever.
-
clean: This removes all downloaded package files. It frees up space but means you can't easily reinstall a package version offline,. -
autoclean: This is the slightly gentler cousin; it only removes package files that are old and can no longer be downloaded, making them completely useless.
sudo apt clean
Purge (Handle with Care!)
If you want an application and its configuration files completely erased (like, gone forever, including files in /etc), you use purge. This is more aggressive than a standard remove.
Important Note: Only use purge if you are absolutely sure you never need those configurations again, especially for core system utilities. When i want to use Purge, always double check everything :)))
3. The Kernel :
Every time Ubuntu updates its kernel (the core of the OS), it typically keeps the old ones around as a backup—just in case the new one causes trouble. This is smart, but if you've done several updates, these kernels pile up, consuming gigabytes of space in /boot. For example, Kernel of Ubuntu 24 is around 1.5GB. Wowww
Always keep the currently running kernel plus at least one or two older, known working kernels.
Listing Your Kernels
First, let's see what you have installed. Look for the packages starting with linux-image and linux-headers,:
dpkg --list | egrep -i --color 'linux-image|linux-headers'
And check which one is currently running (don't delete this one!):
uname -r
How to Remove Them
While autoremove sometimes catches old kernels, the dedicated script purge-old-kernels is far more reliable and efficient for this specific task. You usually get this utility by installing the byobu package:
-
Install the Tool (if needed):
sudo apt install byobu -
Run the Purge: (This example keeps the latest two kernels, removing all older ones)
sudo purge-old-kernels --keep 2
4. Calming Down Storage Filler, The Logs
Log files, usually tucked away in /var/log/, record everything your system does. They are vital for troubleshooting, but they grow continuously and, if unchecked, can strain your server resources and memory.
The Automated Log Boss: Logrotate
Luckily, Linux has a built-in superhero named Logrotate. This utility is pre-installed on Ubuntu and runs automatically (usually daily via a cron job or systemd timer).
Logrotate ensures logs are managed by either renaming or compressing files before they get too large, and it removes or archives the oldest logs to free up space,,. For example, logrotate often compresses files using gzip and limits the number of historical logs kept (like keeping only four weeks' worth of backlogs).
The Safe Manual Fix: Truncation
If you find a huge log file (like syslog) that's rapidly filling up because of a system bug, DO NOT delete it with rm,. If a program is actively writing to a log file, deleting it won't free up space until that program closes the file, which might not happen until reboot,.
The safe trick is to truncate (empty) the file while keeping the file itself intact:
# Example for the main system log
sudo truncate -s 0 /var/log/syslog
Systemd Journal Logs
For logs managed by systemd (which is standard on modern Ubuntu), you can use journalctl to limit their size (e.g., to a maximum of 300 megabytes),:
sudo journalctl --vacuum-size=300M
5. Home Cleanup
Finally, let's look in your personal space—your home directory (~).
-
The Cache (
~/.cache/): Applications store temporary files and caches here, which can pile up,. It is generally safe to delete the *contents* of this folder, though expect some apps (like web browsers) to load slightly slower the first time afterward.
rm -rf ~/.cache/* The Forgotten Folder (
~/Downloads): This is the most common culprit for digital hoarding,. Use the file manager or terminal to check its size and delete large, forgotten ISOs or installer packages.-
The Command Line Trash: If you use the graphical desktop environment, files you delete go to the Trash Bin (
~/.local/share/Trash/). Make sure it's truly empty!
rm -rf ~/.local/share/Trash/*
🛠️ Terminal Command Cheat Sheet
Here are the key commands That i used for my Ubuntu system cleanup. Remember to use sudo where necessary and review any deletions before confirming! And as Michael Jackson said: Remember to always think twice.
| Category | Goal | Command to Run |
|---|---|---|
| Analysis | Find Disk Hogs (Install & Run Interactive Tool) |
sudo apt install ncdu then sudo ncdu /
|
| Package Cleanup | Remove Unused Dependencies | sudo apt autoremove --purge |
| Package Cleanup | Clear Downloaded Package Cache | sudo apt clean |
| Kernel Cleanup | Install Kernel Management Tool | sudo apt install byobu |
| Kernel Cleanup | Remove Old Kernels (Keeps 2 latest versions) | sudo purge-old-kernels --keep 2 |
| Log Management | Limit Systemd Journal Logs (e.g., 300M max) | sudo journalctl --vacuum-size=300M |
| Log Management | Safely Empty Large Active Log File | sudo truncate -s 0 /var/log/syslog |
| User Cleanup | Clear Application/Browser Caches | rm -rf ~/.cache/* |
| User Cleanup | Empty the Trash Bin | rm -rf ~/.local/share/Trash/* |
I will appreciate if you see my other blog post Here.


Top comments (0)