Table of Contents
APT cache can take up disk space on Ubuntu servers over time, especially after frequent package installations and updates. Clearing the APT cache helps free storage without removing installed applications.
On a Linux VPS server, cleaning unused package files is useful when disk space is limited or when the server stores old downloaded package archives.
Step 1: Connect to the Ubuntu Server
Access the server using SSH:
ssh root@your_server_ip
Step 2: Check Current Disk Usage
Before clearing the cache, check available disk space:
df -h
Check how much space the APT cache is using:
du -sh /var/cache/apt
The APT package cache is usually stored inside:
/var/cache/apt/archives
Step 3: Update Package Information
Refresh the package index:
apt update
This makes sure Ubuntu has the latest package information before cleanup.
Step 4: Clear the APT Cache
Remove downloaded package files from the local APT cache:
apt clean
This command clears cached package archives from the server while keeping installed packages untouched.
Step 5: Remove Only Outdated Cached Packages
To remove only old package files that are no longer useful, run:
apt autoclean
This is a safer option when you want to reduce cache size without clearing every downloaded package archive.
Step 6: Remove Unused Dependencies
Remove packages that were installed automatically but are no longer required:
apt autoremove -y
This helps clean old dependencies left behind after package removals or upgrades.
Step 7: Check Disk Space Again
Verify available disk space after cleanup:
df -h
Check the APT cache size again:
du -sh /var/cache/apt
The cache directory should now use less disk space.
Step 8: Combine Cleanup Commands
For a quick cleanup, you can run:
apt clean
apt autoclean
apt autoremove -y
These commands remove cached package files and unused dependencies without deleting active applications.
Step 9: Clear APT Lists Only If Needed
If package list files are taking too much space, you can remove them manually:
rm -rf /var/lib/apt/lists/*
After removing package lists, run:
apt update
This downloads fresh package index files again.
If the server still has low storage after cleanup, check large files and logs before deleting application data. You can also review disk usage regularly to avoid storage-related service issues on Ubuntu VPS environments.
Clearing the APT cache on Ubuntu 24.04 Server is a safe way to recover disk space from old downloaded package files while keeping installed software and system services intact.
