Introduction / Why This Is Needed
The apt package manager in Ubuntu and Debian-based systems stores all downloaded .deb files in the cache directory /var/cache/apt/. Over time, this cache can grow to several gigabytes, especially on systems where packages are frequently updated or newly installed.
Regularly cleaning the apt cache provides three main advantages:
- Freeing up disk space — critical for systems with small SSDs or virtual machines.
- Speeding up
apt update— the manager spends less time processing outdated metadata. - Maintaining system cleanliness — prevents the accumulation of "junk" from removed packages.
This guide will show you how to perform a safe cleanup without damaging your installed software.
Requirements / Preparation
Before you begin, ensure that:
- You have Ubuntu (or a derivative system like Linux Mint) version 20.04 or newer, or Debian 11/12.
- You have access to a terminal.
- Superuser privileges (sudo) are required to execute the commands. If you do not have sudo rights, contact your administrator.
- Recommended: Create a system snapshot or ensure you have a current backup of important data, although cleaning the apt cache does not affect user files.
Step-by-Step Instructions
Step 1: Check the current size of the apt cache
Always start with an assessment. Open a terminal (Ctrl+Alt+T) and run:
sudo du -sh /var/cache/apt/
Example output:
1.2G /var/cache/apt/
This means the cache occupies 1.2 gigabytes. If the value is less than 100 MB, cleanup may be less critical.
Step 2: Clean the entire apt cache (full)
The apt-get clean command will remove all files from the cache, including current package versions.
sudo apt-get clean
After execution, check the directory size again (sudo du -sh /var/cache/apt/). It should be close to 0.
⚠️ Important: After this operation, reinstalling any package will require a full download from the repositories. If you have a slow or metered internet connection, consider the
autocleanoption (Step 3).
Step 3: Clean outdated cache files (safe)
The apt-get autoclean command removes only package files that are no longer available in the repositories (e.g., old versions replaced by new ones). Current packages remain in the cache.
sudo apt-get autoclean
This is a more careful approach that retains the cache for quick reinstallation of current software versions.
Step 4: Remove unused dependencies
When you remove a program via apt remove, its dependencies (libraries, etc.) often remain on the system. The autoremove command finds and removes such "orphaned" packages.
# First, show what will be removed (without confirmation)
sudo apt-get autoremove --dry-run
# If the list is acceptable, perform the actual removal
sudo apt-get autoremove
For an even deeper cleanup (removing configuration files as well), use:
sudo apt-get autoremove --purge
💡 Tip: Always carefully read the package list before confirming. Sometimes
autoremovemay suggest removing a package you use manually (e.g.,python3) if it was installed as a dependency of another.
Step 5: Additional manual cleanup (optional)
If after autoremove you see unnecessary packages in the installed list (apt list --installed), remove them selectively:
# Search for a package by keyword
apt list --installed | grep -i "unneeded_name"
# Remove a specific package
sudo apt-get remove --purge package_name
Verification
- Check the freed space:
sudo du -sh /var/cache/apt/
The value should be significantly smaller than the original. - Check total free space on the system partition:
df -h /
Compare theAvailablevalue before and after the operations. - Ensure the system remains stable:
Try installing a small package (e.g.,
curl):sudo apt-get update sudo apt-get install curl
If the installation completes without errors, everything is fine.
Potential Issues
"No space left on device" error when running commands
If space on / is so low that even apt cannot function, try:
- Manually delete the largest files in
/var/cache/apt/(ifcleanfailed). - Clean other directories:
/tmp, old logs in/var/log/, move or delete large user files.
autoremove suggests removing a needed package
- Solution: Decline the operation (
Nat confirmation). Find which installed package depends on it:apt rdepends <package_name>. If the package is truly needed, install it explicitly:sudo apt-get install <package_name>. After this, it will no longer be considered "unused."
Cache does not clean completely
Ensure you are using sudo. Also, check if any processes are using files in /var/cache/apt/ (e.g., a background apt-daily). In this case, terminate the process or reboot the system.
apt-get update is slower after cleanup
This is temporary: on the first run, update needs to re-download package indexes. In subsequent runs, speed will return to normal as metadata is cached in memory.
Advanced Option: Configuring Automatic Cleanup
For servers and stationary workstations, you can configure automatic cleanup via cron. Open the crontab for editing:
sudo crontab -e
Add a line (for example, to run daily at 3 AM):
0 3 * * * /usr/bin/apt-get autoclean && /usr/bin/apt-get autoremove -y
⚠️ Warning: Automatic
autoremovecan lead to the removal of important packages after system updates. Start with a test period, replacingautoremovewithautoremove --dry-runand analyzing the logs.
Conclusion
Cleaning the apt cache is a simple but important maintenance procedure for Ubuntu/Debian. By performing it periodically (once a month or after major updates), you:
- Free up precious gigabytes on the system partition.
- Speed up the package manager.
- Reduce the risk of errors due to lack of space.
Use apt-get clean for maximum effect or apt-get autoclean for a more cautious cleanup, and autoremove to clean up dependency junk. Regular system care is the key to its stability and performance.