Linux

Cleaning apt Cache in Ubuntu: Free Up Space and Speed Up Your System

This guide explains in detail how to safely clean the apt package manager cache in Ubuntu, remove unused dependencies, and keep the system in optimal condition without risking installed software.

Updated at February 15, 2026
5-10 min
Easy
FixPedia Team
Применимо к:Ubuntu 20.04 LTSUbuntu 22.04 LTSUbuntu 24.04 LTSDebian 11/12

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:

  1. Freeing up disk space — critical for systems with small SSDs or virtual machines.
  2. Speeding up apt update — the manager spends less time processing outdated metadata.
  3. 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 autoclean option (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 autoremove may 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

  1. Check the freed space:
    sudo du -sh /var/cache/apt/
    

    The value should be significantly smaller than the original.
  2. Check total free space on the system partition:
    df -h /
    

    Compare the Available value before and after the operations.
  3. 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:

  1. Manually delete the largest files in /var/cache/apt/ (if clean failed).
  2. 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 (N at 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 autoremove can lead to the removal of important packages after system updates. Start with a test period, replacing autoremove with autoremove --dry-run and 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.

F.A.Q.

Why is a large apt cache dangerous and why should it be cleared?
What is the difference between `apt-get clean` and `apt-get autoclean`?
Can apt cleanup be automated via cron?
What does the `apt-get autoremove` command do and when can it be dangerous?
Why is it necessary to reboot the system or reinstall packages after apt cleanup?

Hints

Check current apt cache size
Clean all apt cache (full)
Clean obsolete cache files (safe)
Remove unused dependencies
Manual check and removal of specific packages
FixPedia

Free encyclopedia for fixing errors. Step-by-step guides for Windows, Linux, macOS and more.

© 2026 FixPedia. All materials are available for free.

Made with for the community