How to Save Disk Space by Clearing APT Cache on Linux

How to Save Disk Space by Clearing APT Cache on Linux

Understanding how to save disk space by clearing apt cache on Linux to improve system performance is essential. We will discuss two popular commands that can be used for this purpose. 

The primary purpose of the apt command is to save disk space by removing unnecessary files on your computer. It can be used for the scheduled and regular computer maintenance, making it essential. 

We will briefly discuss the apt package manager, followed by information about apt-cache. We will discuss the popular commands for removing cache files. Next, we will also provide perspectives on the impact of removing cache files. Also, some graphical user interface (GUI) based tools for clearing the cache.

Introduction to the apt package manager

The apt package manager is used to install applications on Linux computers. You usually install an application in Linux using apt-get or apt command. Whenever you install the application, the apt downloads all the packages and their dependencies in .deb format. These packages are kept in the /var/apt/archives folder. 

These files are kept in /var/cache/apt/archives/partial during the downloading process. After downloading, these packages are moved to the folder /var/cache/apt/archives. After the dependencies are downloaded, the system installs packages from these .deb files.

Introduction to apt-cache

Whenever you install any application on your Linux machine, the apt package manager caches this package so that you don’t have to download it again. If you uninstall the package and try to install it again, it will be installed from the cache.

The cache also increases as the number of applications on your system grows. The cache may reach the size of gigabytes (GB). Naturally, most of these packages are never needed again as they have already been installed. Hence it is essential to delete the files in the apt-cache to save disk space.

Note: You can compare the time to install a package when it is cached versus the time when it is not cached via the following command: 

$time sudo apt install package_name


When cleaning a cache may be necessary?

It depends on the situation, i.e., if clearing the cache is wiser. For instance, you may be getting out of space and wants to clean the cache to have more space to run the software on the system. To determine the space consumed by the cache folder, run the following command on your Terminal:

$sudo du –sh /var/cache/apt/archives 


Sometimes the size of the cache may be in GBs. Therefore in these situations, it is wiser to clear the cache.

Commands to delete the apt-cache

Even though the apt-cache directory can be deleted manually, there are dedicated commands to delete the cache. There are two popular commands for this purpose i.e.

  • apt-auto-clean that can be used to delete old downloaded archive files
  • apt-clean that can be used to delete downloaded archive files

We will now discuss examples for each of the above two commands. Follow the steps below to clear apt-cache. To perform the steps below, you need to have a system with Linux installed and a root account or an account with sudo privileges.

Update your system cache

The very first step is to update your system’s apt-cache. Open your Terminal and type the following command on the Terminal:

$sudo apt update


Upgrade your whole system

Now upgrade your whole system by using the following command:

$sudo apt upgrade


Deleting the old archive

To delete the old archive, type the following command on your Terminal:

$sudo apt autoclean


This command will remove all the unneeded packages.

Deleting all the files of apt-cache

If you want to clean your apt-cache and get rid of all the packages, you can run the following command:

$sudo apt clean


Is it safe to delete the cache?

There are a few natural questions that may arise regarding the apt-cache, such as: 

  • Whether it is safe to delete the apt-cache? 
  • Is there any harm in deleting cache files?
  • What is the impact of deleting the apt-cache files?

We will answer these questions in detail. First of all, it is entirely safe to remove the apt-cache file. There is no harm in removing these files. It doesn’t impact you negatively in any way on your system. The only effect is that if you uninstall the package and retry to install it, it may take some time to download the file from the internet.

Graphical user interface (GUI) tools for clearing apt-cache

Besides the command line tools for clearing the cache, there are several GUI-based tools for clearing the cache. Some of these tools are:

  • Stacer
  • Bleachbit

In this article, we have discussed the commands on how to save disk space by clearing apt cache on Linux. We discussed two commands, one to clear the old packages and another to remove the cache. We also mentioned two GUI-based tools for clearing up the cache.

If this guide helped you, please share it.

Leave a Reply
Related Posts