I noticed that /var/cache/apt/archives/ taking lots of disk space on my AWS EC2 cloud server powered by Debian Linux. Can I delete /var/cache/apt/archives/ folder? Is it safe to remove all files? Do we absolutely necessary to keep all these files?
APT is a set of command found on Debian based operating systems. One can use apt-get command/apt command/dpkg command and others to manage the installation, update, and removal of packages. Another tool called apt-cache gives read-only access to the APT package cache. One can use apt-cache to gather useful information from software package metadata. This page explains the usage of /var/cache/apt/archives/ directory and how to safely delete /var/cache/apt/archives files stored in Ubuntu or Debian Linux to free up occupying enormous space.
Command to delete /var/cache/apt/archives in Ubuntu/Debian Linux
All .deb packages are retrieved from the Internet and stored in /var/cache/apt/archives/ directory. Similarly, /var/cache/apt/archives/partial/ directory work as storage area for package files in transit. To see files, run:
cd /var/cache/apt/archives/
ls -l
ls -l | tail -10
Use the du command to estimate file space usage and summarize disk usage of /var/cache/apt/archives/, recursively for directories.
sudo du -ch /var/cache/apt/archives/
Sample outputs:
4.0K /var/cache/apt/archives/partial 128M /var/cache/apt/archives/ 128M total
On my personal system 128M disk spaced used by the folder. In other words, APTs cached files are located in /var/cache/apt/archives/ directory.
Is it safe to remove all files?
Yes. See below how to erase those files.
Do we absolutely necessary to keep all these files?
No. See below how to free up disk space.
apt-get/apt high level package management utility
Let us see some common examples.
Installing a package
sudo apt install {package-name}
For example, install squid3 package, enter:
sudo apt install squid3
You will see downloaded file in /var/cache/apt/archives/
ls -l /var/cache/apt/archives/squid*
Removing a package
To remove a package without removing its confi files, use:
sudo apt remove {package-name}
In order to remove a package and its config files, run:
sudo apt remove --purge {package-name}
In this example, delete the nginx and its configuration files:
sudo apt remove --purge nginx
However, this will not remove any downloaded files from the /var/cache/apt/archives/ directory.
How to clear the APT cache and delete everything from /var/cache/apt/archives/
The clean command clears out the local repository of retrieved package files. It removes everything but the lock file from /var/cache/apt/archives/ and /var/cache/apt/archives/partial/. The syntax is:
sudo apt clean
OR
sudo apt-get clean
Delete all useless files from the APT cache
The syntax is as follows to delete /var/cache/apt/archives/:
sudo apt autoclean
OR
sudo apt-get autoclean
From the apt-get man page:
Like clean, autoclean clears out the local repository of retrieved package files. The difference is that it only removes package files that can no longer be downloaded, and are largely useless. This allows a cache to be maintained over a long period without it growing out of control. The configuration option APT::Clean-Installed will prevent installed packages from being erased if it is set to off.
Verify that files are removed from /var/cache/apt/archives/ safely using the du command:
sudo du -ch /var/cache/apt/archives/
Conclusion
This page explained APT cache and how to use the clean and autoclean apt-get command that clears out the downloaded package files from/var/cache/apt/archives/ folder. In short, use the sudo apt clean and sudo apt autoclean to free up disk space as part of scheduled maintenance on your Debian or Ubuntu Linux server.