Table of Contents#
- Why Unused Packages Drain Your Ubuntu’s Performance
- Prerequisites
- Step-by-Step Guide to Clean Unused Packages with Terminal
- Advanced Cleaning Tips for Power Users
- Post-Cleanup Checks to Ensure System Health
- Precautions to Avoid Accidental Damage
- Conclusion
- References
1. Why Unused Packages Drain Your Ubuntu’s Performance#
Before diving into commands, let’s understand how unused packages hurt your system:
a. Disk Space Bloat#
Every time you install an app, Ubuntu stores cached .deb files, dependent libraries, and configuration files. When you uninstall an app, many of these residual files stick around. Over months, this adds up—old kernels alone can take up 1-2 GB of space in the /boot partition.
b. Slow Package Manager Operations#
A cluttered APT package database means apt (Ubuntu’s default package manager) has to sift through thousands of unused entries to find what it needs. This makes updates, installs, and searches take longer.
c. System Instability Risks#
Outdated or orphaned packages (dependencies left behind after their parent app is removed) can cause conflicts with newer software. This might lead to unexpected crashes, broken apps, or errors during system updates.
d. Longer Boot Times#
Old kernel versions listed in the GRUB menu force your system to load more options during boot, increasing startup time. They also take up space in the /boot partition, which is often small and can fill up quickly.
2. Prerequisites#
Before you start cleaning, ensure you have:
- Basic Terminal Knowledge: You’ll need to type commands into the terminal. Open it quickly with the shortcut
Ctrl + Alt + T. - Sudo Privileges: Most cleaning commands require admin access. You’ll need to prefix them with
sudoand enter your password when prompted. - Backup Important Data: While the commands in this guide are safe when followed correctly, it’s always wise to back up critical files (e.g., documents, photos) to an external drive or cloud storage.
3. Step-by-Step Guide to Clean Unused Packages with Terminal#
Let’s start with the core commands to clean up your system. Each step includes an explanation so you understand its purpose.
Step 1: Update Package Lists First#
Before cleaning, make sure your system has the latest information about package states. This ensures apt knows which packages are unused or outdated.
sudo apt updateType your password (it won’t be visible on the screen) and press Enter.
Step 2: Clean APT Cache#
Ubuntu’s APT package manager stores downloaded .deb files in /var/cache/apt/archives/ to avoid re-downloading them. Over time, this cache grows large and includes outdated files.
Option A: Remove All Cached .deb Files#
Use this command to clear the entire cache (safe when you need maximum space):
sudo apt cleanOption B: Remove Only Outdated Cached Files#
If you want to keep cached files for currently available packages (to speed up future installs), use:
sudo apt autocleanStep 3: Remove Unused Dependencies#
When you install an app, Ubuntu often installs additional libraries (dependencies) to make it work. If you uninstall the app, these dependencies are no longer needed—unless another app uses them.
sudo apt autoremoveThis command lists all unused dependencies and asks for confirmation before removing them. To also delete their configuration files (freeing up more space and reducing clutter), add the --purge flag:
sudo apt autoremove --purgePro Tip: If you see a package in the list that you want to keep (e.g., a library you use for a custom script), cancel the command with Ctrl + C, then mark it as manually installed:
sudo apt-mark manual <package-name>Re-run autoremove after marking the package.
Step 4: Remove Orphaned Packages#
apt autoremove catches most unused dependencies, but some orphaned packages (dependencies whose parent app was never installed or was removed long ago) might slip through the cracks. To find and remove them, use deborphan:
First, install deborphan:
sudo apt install deborphanThen, list all orphaned packages:
sudo deborphanTo remove them (including config files):
sudo apt purge $(deborphan)Step 5: Remove Old Kernel Versions#
Old kernel versions are one of the biggest space hogs in Ubuntu. They take up space in /boot and slow down the GRUB menu.
First, check your current running kernel (don’t remove this one!):
uname -rOutput example: 5.15.0-76-generic
Next, list all installed kernels:
dpkg --list | grep linux-imageLook for lines starting with ii (meaning installed) that don’t match your current kernel version.
Remove old kernels and their headers (replace <version> with the kernel number):
sudo apt purge linux-image-<version>-generic linux-headers-<version>-genericAlternatively, use this command to automatically remove all unused kernels (safer for beginners):
sudo apt autoremove --purgeAfter removing kernels, update the GRUB boot menu:
sudo update-grubStep 6: Fix Broken Packages#
If your system has partially installed or broken packages, they can cause errors during cleaning. Fix them with:
sudo apt --fix-broken installTo reconfigure any broken package configurations:
sudo dpkg --configure -a4. Advanced Cleaning Tips for Power Users#
For even deeper cleaning, try these advanced commands:
a. Remove Residual Configuration Files#
When you uninstall a package with apt remove, its configuration files often remain. These are marked with rc in the dpkg list. To remove them:
sudo apt purge $(dpkg --list | grep '^rc' | awk '{print $2}')b. Use BleachBit (Terminal Version)#
BleachBit is a popular cleaning tool that goes beyond package cleaning—it removes browser caches, system logs, temp files, and more. Install the terminal version:
sudo apt install bleachbitClean system-wide bloat (safe but thorough):
sudo bleachbit --clean systemTo clean user-specific data (e.g., browser cache):
bleachbit --clean userc. Use Aptitude for Granular Control#
Aptitude is a more advanced package manager that offers better control over orphaned packages. Install it:
sudo apt install aptitudeList orphaned packages:
aptitude search '~o'Remove them (including config files):
sudo aptitude purge '~o'5. Post-Cleanup Checks to Ensure System Health#
After cleaning, verify your system is in good shape:
-
Check Free Disk Space:
df -hLook at the
/partition to see how much space you freed up. -
Verify No Broken Packages:
sudo apt checkIf no errors appear, your package database is healthy.
-
Reboot Your System: If you removed old kernels or made major changes, reboot to apply updates:
sudo reboot
6. Precautions to Avoid Accidental Damage#
While these commands are safe if used correctly, follow these rules to stay out of trouble:
- Never Use
rm -rffor Package Cleaning: This command deletes files permanently and can erase critical system files if misused. Stick toaptcommands. - Double-Check Packages Before Removing: Always read the list of packages to be removed by
autoremoveorpurgebefore pressingY. - Don’t Remove Your Current Kernel: Always run
uname -rto confirm your active kernel before purging any kernel packages. - Avoid Over-Cleaning: Use
autocleaninstead ofcleanfor regular maintenance.cleanis only necessary when you need to free up a lot of space. - Backup Config Files: If you’re purging packages with
--purge, back up any custom config files (e.g.,~/.bashrc,/etc/nginx/nginx.conf) first.
7. Conclusion#
Cleaning unused packages is one of the easiest and most effective ways to speed up Ubuntu without reinstalling the system. By using the terminal commands in this guide, you can free up disk space, reduce system clutter, speed up package manager operations, and improve overall stability.
Make cleaning a regular habit—do it once a month or after major system updates to keep your Ubuntu running like new. For beginners, sticking to apt autoremove, autoclean, and kernel removal steps will suffice. Power users can explore advanced tools like deborphan and BleachBit for deeper cleaning.