如何清除 APT 缓存来回收宝贵的磁盘空间
apt
![](/data/attachment/album/202011/04/103035mc3b3mmimbqjj3fc.png)


如何清除 APT 缓存？你只需使用这个 [apt-get 命令](https://itsfoss.com/apt-get-linux-guide/)选项：



```
sudo apt-get clean

```

但是，清理 APT 缓存不仅仅是运行上面的命令。


在本教程中，我将解释什么是 APT 缓存、为什么会使用它、为什么你要清理它，以及关于清理 APT 缓存你应该知道的其他事情。


我将在这里使用 Ubuntu 作为参考，但由于这是关于 APT 的，因此它也适用于 [Debian](https://www.debian.org/) 和其他基于 Debian 和 Ubuntu 的发行版，比如 Linux Mint、Deepin 等等。


### 什么是 APT 缓存？为什么要使用它？


当你使用 `apt-get` 或 [apt 命令](https://itsfoss.com/apt-command-guide/)安装一个软件包时（或在软件中心安装 DEB 包），APT [包管理器](https://itsfoss.com/package-manager/)会以 .deb 格式下载软件包及其依赖关系，并将其保存在 `/var/cache/apt/archives` 文件夹中。


![](/data/attachment/album/202011/04/103054a2xq9vu3s1q414x9.png)


下载时，`apt` 将 deb 包保存在 `/var/cache/apt/archives/partial` 目录下。当 deb 包完全下载完毕后，它会被移到 `/var/cache/apt/archives` 目录下。


下载完包的 deb 文件及其依赖关系后，你的系统就会[从这些 deb 文件中安装包](https://itsfoss.com/install-deb-files-ubuntu/)。


现在你明白缓存的用途了吧？系统在安装软件包之前，需要一个地方把软件包文件存放在某个地方。如果你了解 [Linux 目录结构](https://linuxhandbook.com/linux-directory-structure/)，你就会明白，`/var/cache` 是合适的地方。


#### 为什么安装包后要保留缓存？


下载的 deb 文件在安装完成后并不会立即从目录中删除。如果你删除了一个软件包，然后重新安装，你的系统会在缓存中查找这个软件包，并从这里获取它，而不是重新下载（只要缓存中的软件包版本与远程仓库中的版本相同）。


这样就快多了。你可以自己尝试一下，看看一个程序第一次安装，删除后再安装需要多长时间。你可以[使用 time 命令来了解完成一个命令需要多长时间](https://linuxhandbook.com/time-command/)：`time sudo apt install package_name`。


我找不到任何关于缓存保留策略的内容，所以我无法说明 Ubuntu 会在缓存中保留下载的包多长时间。


#### 你应该清理 APT 缓存吗？


这取决于你。如果你的根目录下的磁盘空间用完了，你可以清理 APT 缓存来回收磁盘空间。这是 [Ubuntu 上释放磁盘空间的几种方法](https://itsfoss.com/free-up-space-ubuntu-linux/)之一。


使用 [du 命令](https://linuxhandbook.com/find-directory-size-du-command/)检查缓存占用了多少空间：


![](/data/attachment/album/202011/04/103043tv4vkrofk0kmzdjd.png)


有的时候，这可能会占用几百兆，如果你正在运行一个服务器，这些空间可能是至关重要的。


#### 如何清理 APT 缓存？


如果你想清除 APT 缓存，有一个专门的命令来做。所以不要去手动删除缓存目录。只要使用这个命令就可以了：



```
sudo apt-get clean

```

这将删除 `/var/cache/apt/archives` 目录的内容（除了锁文件）。以下是 `apt-get clean` 命令模拟删除内容：


![](/data/attachment/album/202011/04/102950m84n2zaz8f42084n.png)


还有一个命令是关于清理 APT 缓存的：



```
sudo apt-get autoclean

```

与 `clean` 不同的是，`autoclean` 只删除那些无法从仓库中下载的包。


假设你安装了包 xyz。它的 deb 文件仍然保留在缓存中。如果现在仓库中有新的 xyz 包，那么缓存中现有的这个 xyz 包就已经过时了，没有用了。`autoclean` 选项会删除这种不能再下载的无用包。


#### 删除 apt 缓存安全吗？


是的，清除 APT 创建的缓存是完全安全的。它不会对系统的性能产生负面影响。也许如果你重新安装软件包，下载时间会更长一些，但也仅此而已。


再说一次，使用 `apt-get clean` 命令。它比手动删除缓存目录更快、更简单。


你也可以使用像 [Stacer](https://itsfoss.com/optimize-ubuntu-stacer/) 或 [Bleachbit](https://itsfoss.com/use-bleachbit-ubuntu/) 这样的图形工具来实现这个目的。


#### 总结


在写这篇文章的时候，新的 `apt` 命令没有这样的内置选项。不过，为了保持向后的兼容性，仍然可以运行 `apt clean` （内部应该是运行了 `apt-get clean`）。请参考这篇文章来[了解 apt 和 apt-get 的区别](https://itsfoss.com/apt-vs-apt-get-difference/)。


我希望你觉得这个关于 APT 缓存的解释很有趣。虽然这不是什么必要的东西，但了解这些小东西会让你对你的 Linux 系统更加了解。


欢迎你在评论区提出反馈和建议。




---


via: <https://itsfoss.com/clear-apt-cache/>


作者：[Abhishek Prakash](https://itsfoss.com/author/abhishek/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

How do you clear the apt cache? You simply use this [apt-get command](https://itsfoss.com/apt-get-linux-guide/) option:

`sudo apt-get clean`


But there is more to cleaning apt cache than just running the above command.

In this tutorial, I’ll explain what is apt cache, why is it used, why you would want to clean it and what other things you should know about purging apt cache.

I am going to use Ubuntu here for reference but since this is about apt, it is applicable to [Debian](https://www.debian.org/?ref=itsfoss.com) and other Debian and Ubuntu-based distributions like Linux Mint, Deepin and more.

## What is apt cache? Why is it used?

When you install a package using apt-get or [apt command](https://itsfoss.com/apt-command-guide/) (or DEB packages in the software center), the apt [package manager](https://itsfoss.com/package-manager/) downloads the package and its dependencies in .deb format and keeps it in /var/cache/apt/archives folder.

![Apt Get Clean Cache](https://itsfoss.com/content/images/wordpress/2020/10/apt-get-clean-cache-800x470.png)

While downloading, apt keeps the deb package in /var/cache/apt/archives/partial directory. When the deb package is downloaded completely, it is moved out to /var/cache/apt/archives directory.

Once the deb files for the package and its dependencies are downloaded, your system [installs the package from these deb files](https://itsfoss.com/install-deb-files-ubuntu/).

Now you see the use of cache? The system needs a place to keep the package files somewhere before installing them. If you are aware of the [Linux directory structure](https://linuxhandbook.com/linux-directory-structure/?ref=itsfoss.com), you would understand that /var/cache is the appropriate here.

### Why keep the cache after installing the package?

The downloaded deb files are not removed from the directory immediately after the installation is completed. If you remove a package and reinstall it, your system will look for the package in the cache and get it from here instead of downloading it again (as long as the package version in the cache is the same as the version in remote repository).

This is much quicker. You can try this on your own and see how long a program takes to install the first time, remove it and install it again. You can [use the time command to find out how long does it take to complete a command](https://linuxhandbook.com/time-command/?ref=itsfoss.com): **time sudo apt install package_name**.

I couldn’t find anything concrete on the cache retention policy so I cannot say how long does Ubuntu keep the downloaded packages in the cache.

### Should you clean apt cache?

It depends on you. If you are running out of disk space on root, you could clean apt cache and reclaim the disk space. It is one of the [several ways to free up disk space on Ubuntu](https://itsfoss.com/free-up-space-ubuntu-linux/).

Check how much space the cache takes with the [du command](https://linuxhandbook.com/find-directory-size-du-command/?ref=itsfoss.com):

![Apt Cache Archive Size](https://itsfoss.com/content/images/wordpress/2020/10/apt-cache-archive-size-800x233.png)

Sometime this could go in 100s of MB and this space could be crucial if you are running a server.

### How to clean apt cache?

If you want to clear the apt cache, there is a dedicated command to do that. So don’t go about manually deleting the cache directory.

You may think it is [apt-cache command](https://itsfoss.com/apt-cache-command/) but that’s deceiving. Simply use the apt-get command with clean as argument:

`sudo apt-get clean`


This will remove the content of the /var/cache/apt/archives directory (except the lock file). Here’s a dry run (simulation) of what the apt-get clean command deletes:

![Apt Get Clean Ubuntu](https://itsfoss.com/content/images/wordpress/2020/10/apt-get-clean-ubuntu-800x339.png)

There is another command that deals with cleaning the apt cache:

`sudo apt-get autoclean`


Unlike clean, autoclean only removes the packages that are not possible to download from the repositories.

Suppose you installed package xyz. Its deb files remain in the cache. If there is now a new version of xyz package available in the repository, this existing xyz package in the cache is now outdated and useless. The autoclean option will delete such useless packages that cannot be downloaded anymore.

### Is it safe to delete apt cache?

![Clear Apt Cache](https://itsfoss.com/content/images/wordpress/2020/10/Clear-Apt-Cache.png)

Yes. It is completely safe to clear the cache created by apt. It won’t negatively impact the performance of the system. Maybe if you reinstall the package it will take a bit longer to download but that’s about it.

Again, use the apt-get clean command. It is quicker and easier than manually deleting cache directory.

You may also use graphical tools like [Stacer](https://itsfoss.com/optimize-ubuntu-stacer/) or [Bleachbit](https://itsfoss.com/use-bleachbit-ubuntu/) for this purpose.

### Conclusion

At the time of writing this article, there is no built-in option with the newer apt command. However, keeping backward compatibility, **apt clean** can still be run (which should be running apt-get clean underneath it). Please refer to this article to

[know the difference between apt and apt-get](https://itsfoss.com/apt-vs-apt-get-difference/).

I hope you find this explanation about apt cache interesting. It is not something essential but knowing this little things make you more knowledgeable about your Linux system.

I welcome your feedback and suggestions in the comment section.