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
.debpackages 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
.debfiles 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
- Disk space: Run the command from step 4 and ensure the size of the
/var/cache/apt/folder has decreased significantly. - APT functionality: Check that the package manager is working. Update the package list:
There should be no errors. If the command completed successfully, the system is fine.sudo apt update - Free space: Check how much space has been freed up on the system partition (usually
/):
Compare thedf -h /Availablevalue before and after cleaning.
Potential Issues
E: Could not open lock file /var/lib/dpkg/lock-frontend
Cause: Another process (e.g.,aptorunattended-upgrades) is already using APT.
Solution: Wait for background updates to finish or terminate the process withsudo 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 withsudo. Manually check the contents:ls -la /var/cache/apt/archives/. apt updatebecame slower after cleaning
Cause: This is normal. On the first run afterclean, 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 usesudoforapt cleanandapt autocleancommands.
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
- Via
unattended-upgrades(if installed): Open the config/etc/apt/apt.conf.d/50unattended-upgradesand 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. - 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. - For servers without
unattended-upgrades: Create a script/usr/local/bin/apt-cache-clean.sh:#!/bin/bash apt autoclean -y
Grant permissions withsudo chmod +x /usr/local/bin/apt-cache-clean.shand 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.