跳到正文
Ubuntu / Linux

Free Up Disk Space on Ubuntu: Clean Unused Packages with apt autoremove

Run sudo apt autoremove --purge, then sudo apt clean. Check the result with df -h.

测试环境 Ubuntu 24.04 LTS, 22.04 LTS

Every install and upgrade leaves something behind: libraries pulled in as dependencies, old kernels and downloaded .deb files. On small cloud servers that can fill /boot or the root disk. Ubuntu’s package manager can clean it up safely.

Step 1 – Check what will be removed

Terminal
df -h /nsudo apt autoremove u002du002ddry-run

Read the list. It should contain only packages that were installed automatically and are no longer needed by anything.

Step 2 – Remove unused packages

Terminal
sudo apt autoremove u002du002dpurge

--purge also removes their leftover configuration files. Confirm with Y.

Step 3 – Clear the package cache

Terminal
sudo apt clean

This deletes downloaded installers in /var/cache/apt/archives; apt downloads them again if ever needed.

Step 4 – Optional: shrink old logs

Terminal
journalctl u002du002ddisk-usagensudo journalctl u002du002dvacuum-time=14d

Step 5 – See what you saved

Terminal
df -h /

Read before you confirm

If autoremove proposes removing something you use (for example after you uninstalled a meta-package), mark it as manually installed first: sudo apt-mark manual <package>. On shared servers, review the list carefully – removing a library another site depends on breaks it.

Make it routine

Run sudo apt update && sudo apt upgrade && sudo apt autoremove --purge during regular maintenance, or put it in a script with logging.

Inder Kochar

作者

Inder Kochar

系统与网络工程师 – 我把为客户使用的解决方案整理成文,并在真实系统上测试过。遇到类似问题了吗?

继续阅读

提出问题或分享您的解决方法

您的邮箱不会公开。评论需经审核——请围绕主题发言。