LinuxLow

APT Cache Cleanup in Ubuntu: A Complete Guide

The APT package manager cache in Ubuntu can take up gigabytes of space. In this guide, you'll learn how to safely clean the cache, free up disk space, and set up automatic system maintenance.

Updated at February 16, 2026
5-10 min
Easy
FixPedia Team
Применимо к:Ubuntu 20.04 LTSUbuntu 22.04 LTSUbuntu 24.04 LTSDebian and derivatives

Introduction / Why This Is Needed

The APT (Advanced Package Tool) package manager in Ubuntu and other distributions based on it stores all downloaded package installation files (.deb files) in a special folder /var/cache/apt/. This cache is useful when reinstalling packages or updating the system—there's no need to download the same files from the internet every time.

However, over time the cache can grow to several gigabytes, especially on systems that frequently update software. Clearing the cache is a simple and safe way to free up space on the system partition (often / or /var), which is particularly relevant for SSDs with limited capacity or virtual machines.

This guide will show you how to properly clear the APT cache without damaging the system and how to set up automatic maintenance to keep it clean.

Requirements / Preparation

Before you begin, ensure that:

  • You have access to an account with sudo privileges (administrator).
  • You are working in a terminal (Ctrl+Alt+T or via SSH).
  • You have the standard APT package manager installed (it is included by default in all official Ubuntu flavours).
  • A stable internet connection is recommended in case you need to re-download metadata (apt update) or packages after cleaning.

Step-by-Step Instructions

Step 1: Check the Current Size of the APT Cache

First, let's assess how much space the cache occupies. This will help understand the scale of the problem.

sudo du -sh /var/cache/apt/

Example output:

1.2G    /var/cache/apt/

In this case, the cache occupies 1.2 gigabytes. If the output shows 0 or a few kilobytes, cleaning may not be necessary.

Step 2: Clean the Cache Completely (apt clean)

This command removes all without exception files from the cache folders (/var/cache/apt/archives/ and /var/cache/apt/archives/partial/).

sudo apt clean

What happens:

  • The system will prompt for your sudo password.
  • All downloaded .deb packages are permanently deleted.
  • After this, the /var/cache/apt/archives/ folder will be empty (or contain only lock files).

⚠️ Important: After apt clean, when you reinstall any package, its installation file will be downloaded again from the repositories. This will not break the system, but it will increase traffic during subsequent operations.

Step 3: Clean Only Obsolete Packages (apt autoclean) — An Alternative Option

If you want to retain the ability to quickly reinstall the latest package versions without re-downloading them, use a gentler method.

sudo apt autoclean

What happens:

  • APT analyzes the cache and compares the list of available .deb files with the current list of packages in the repository.
  • Only files that are no longer available in the repository (e.g., old versions replaced by new ones) are deleted.
  • The most recent package versions remain in the cache.

This is a good compromise between freeing up space and convenience.

Step 4: Verify the Cleaning Result

Immediately after any of the above commands, check how much space is left.

sudo du -sh /var/cache/apt/

Expected output after clean or autoclean:

12K    /var/cache/apt/

If the size hasn't changed, the cache may already have been empty or you were working on a system that was just updated and the cache hasn't filled up yet.

Verification of Results

  1. Disk space: Run the command from step 4 and ensure the size of the /var/cache/apt/ folder has decreased significantly.
  2. APT functionality: Check that the package manager is working. Update the package list:
    sudo apt update
    
    There should be no errors. If the command completed successfully, the system is fine.
  3. Free space: Check how much space has been freed up on the system partition (usually /):
    df -h /
    
    Compare the Available value before and after cleaning.

Potential Issues

  • E: Could not open lock file /var/lib/dpkg/lock-frontend
    Cause: Another process (e.g., apt or unattended-upgrades) is already using APT.
    Solution: Wait for background updates to finish or terminate the process with sudo killall apt apt-get.
  • Cache doesn't clear, size doesn't change
    Cause: The cache is already empty, or you specified the path incorrectly.
    Solution: Ensure you are running the commands with sudo. Manually check the contents: ls -la /var/cache/apt/archives/.
  • apt update became slower after cleaning
    Cause: This is normal. On the first run after clean, APT downloads all repository metadata again. Subsequent calls will use the metadata cache and will be fast.
  • Access error to /var/cache/apt/ without sudo
    Cause: This folder belongs to root and requires elevated privileges.
    Solution: Always use sudo for apt clean and apt autoclean commands.

Additional Tips and Automation

To maintain a clean system, it is recommended to run sudo apt autoclean periodically (once a month). If you are sure you will never need to reinstall old packages, you can use sudo apt clean quarterly.

Setting Up Automatic Cleaning

  1. Via unattended-upgrades (if installed): Open the config /etc/apt/apt.conf.d/50unattended-upgrades and find the line //Unattended-Upgrade::Remove-Unused-Kernel-Programming "false";. Next to it, add:
    APT::Clean-Installed "true";
    

    This will automatically remove obsolete packages after each automatic update.
  2. Via cron (simple option): Add a job to crontab (sudo crontab -e):
    0 4 * * 0 /usr/bin/apt autoclean -y
    

    This task will run every Sunday at 4:00 AM.
  3. For servers without unattended-upgrades: Create a script /usr/local/bin/apt-cache-clean.sh:
    #!/bin/bash
    apt autoclean -y
    

    Grant permissions with sudo chmod +x /usr/local/bin/apt-cache-clean.sh and add it to cron.

Important: Do not configure apt clean in cron for frequent execution (e.g., daily), as this may lead to unnecessary package downloads during frequent updates.

F.A.Q.

What's the difference between apt clean and apt autoclean?
Is it safe to delete the cache? Will it break the system?
How to set up automatic APT cache cleanup?
Why is apt update slower after clearing the cache?

Hints

Check the current APT cache size
Clean the cache completely (apt clean)
Clean only obsolete packages (apt autoclean)
Check the cleanup result
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