Clean Up Disk Space From Old Packages
Disk space management is a perennial concern for system administrators, especially on servers with constrained storage or those running long-term without…
Disk space management is a perennial concern for system administrators, especially on servers with constrained storage or those running long-term without regular maintenance. Accumulated package cache, orphaned dependencies, and old kernel versions can consume significant amounts of disk space, impacting system performance and stability. This article details methods for reclaiming disk space by managing package-related files across Debian/Ubuntu (APT), Red Hat/CentOS/Fedora (DNF/YUM), and Alpine Linux (APK) systems.
Understanding the various components of package management—from downloaded archives to installed libraries and kernels—is crucial for effective cleanup without compromising system integrity. We'll cover specific commands and best practices to identify and remove unneeded data.
APT (Debian, Ubuntu, Mint)
The Advanced Package Tool (APT) on Debian-based systems stores downloaded .deb packages and manages dependencies. Over time, the cache can grow, and uninstalled packages may leave behind configuration files or unused libraries.
Clearing the Package Cache
APT stores downloaded .deb package files in /var/cache/apt/archives/. These files are kept even after the packages are installed, allowing for reinstallation without re-downloading. While convenient, this cache can consume gigabytes of space.
apt clean: This command removes all downloaded.debpackage files from the local repository cache (/var/cache/apt/archives/). It's a safe operation and does not affect currently installed packages.apt autoclean: This command only removes.debfiles for packages that are no longer available for download from the configured repositories (e.g., older versions that have been superseded). It's less aggressive thanapt cleanbut still helps manage cache size.
# Check current cache size (before clean)
sudo du -sh /var/cache/apt/archives/
# Clean up all downloaded package files
sudo apt clean
# Check cache size again (after clean)
sudo du -sh /var/cache/apt/archives/
Removing Orphaned Dependencies and Configuration Files
When packages are installed, they often pull in other packages as dependencies. If the main package is later removed, its dependencies might remain installed, becoming "orphaned."
apt autoremove: This command removes packages that were automatically installed to satisfy dependencies for other packages and are no longer needed. It's a fundamental tool for cleaning up old, unused libraries.apt purge <package_name>: While not directly for bulk cleanup, usingpurgeinstead ofremovewhen uninstalling a package ensures that its configuration files, typically stored in/etc/, are also deleted. For example,sudo apt purge nginxwill remove Nginx and its configuration, whereassudo apt remove nginxwould leave configuration files behind.apt autoremove --purge: This is a powerful combination. It removes orphaned dependencies identified byautoremoveAND purges their configuration files. This is highly recommended for thorough cleanup.
# Identify and remove orphaned dependencies
sudo apt autoremove
# Identify, remove, and purge orphaned dependencies and their config files
sudo apt autoremove --purge
Managing Old Kernel Versions
Debian/Ubuntu systems often keep several older kernel versions installed as a fallback. While useful for recovery, keeping too many can consume hundreds of megabytes or even gigabytes. You should always keep at least two, preferably three, kernel versions: the current one, the previous one, and a known-good stable one.
To identify installed kernels:
dpkg -l | grep linux-image-
dpkg -l | grep linux-headers-
To remove old kernels (replace x.y.z-N with the actual version numbers you want to remove, ensuring you DO NOT remove the currently running kernel, identified by uname -r):
sudo apt purge linux-image-x.y.z-N-generic
sudo apt purge linux-headers-x.y.z-N-generic
After manually purging old kernel packages, run sudo update-grub to update the GRUB bootloader configuration.
DNF/YUM (Red Hat, CentOS, Fedora)
DNF (Dandified YUM) is the next-generation package manager for RPM-based distributions, superseding YUM. Many commands are similar, but DNF offers improved dependency resolution and performance.
Clearing the Package Cache
DNF stores downloaded RPM packages and metadata in /var/cache/dnf/ (or /var/cache/yum/ for older YUM systems). This cache can also grow considerably.
dnf clean packages: Removes all cached RPM packages from/var/cache/dnf/*/packages/.dnf clean metadata: Removes cached repository metadata (XML, SQLite, etc.) used for dependency resolution.dnf clean all: The most comprehensive option, removing all cached packages and metadata. This is the recommended command for a thorough cleanup.
# Check current cache size
sudo du -sh /var/cache/dnf/
# Clean all cached packages and metadata
sudo dnf clean all
# Check cache size again
sudo du -sh /var/cache/dnf/
Removing Orphaned Dependencies and Configuration Files
Similar to APT, DNF can identify and remove packages that were installed as dependencies but are no longer required by any explicitly installed package.
dnf autoremove: Removes packages that were installed as dependencies and are no longer needed by any installed applications.- Unlike APT, DNF/YUM typically handles configuration files differently. When a package is uninstalled with
dnf remove <package_name>, its configuration files are usually left behind in case of reinstallation. There isn't a direct DNF equivalent to APT'spurgefor auto-removing config files upon package removal. You often need to manually locate and delete them (e.g.,/etc/<package>).
# Remove orphaned dependencies
sudo dnf autoremove
Managing Old Kernel Versions
RHEL-based systems also keep multiple kernel versions. The default behavior is often to keep the current kernel plus two previous versions. You can configure this behavior in /etc/dnf/dnf.conf (or /etc/yum.conf) by setting the installonly_limit parameter (e.g., installonly_limit=2 to keep the current and one previous kernel).
To list installed kernels:
rpm -qa | grep kernel
To remove old kernels, first identify the specific packages (e.g., kernel-core-x.y.z, kernel-modules-x.y.z, kernel-devel-x.y.z) you want to remove, ensuring you keep the active kernel (uname -r) and at least one fallback. You'll typically want to remove the kernel-core, kernel-modules, and kernel-headers/kernel-devel for each old version.
# Example: Removing kernel 4.18.0-305.10.2.el8_4
sudo dnf remove kernel-core-4.18.0-305.10.2.el8_4 kernel-modules-4.18.0-305.10.2.el8_4 kernel-headers-4.18.0-305.10.2.el8_4
After removal, DNF will automatically update GRUB.
APK (Alpine Linux)
Alpine Linux, known for its small footprint, uses the APK package manager. While its cache tends to be smaller, it still benefits from cleanup.
Clearing the Package Cache
APK stores downloaded packages in /var/cache/apk/.
apk cache clean: Removes all cached package files from/var/cache/apk/.
# Check cache size
sudo du -sh /var/cache/apk/
# Clean package cache
sudo apk cache clean
# Check cache size again
sudo du -sh /var/cache/apk/
Removing Orphaned Dependencies
APK also tracks dependencies, and apk del can be used to remove packages and their dependencies.
apk autoremove(orapk fix --autoremovein older versions): This command removes packages that were automatically installed as dependencies and are no longer required by any explicitly installed package.
# Remove orphaned dependencies
sudo apk autoremove
General Cleanup Strategies and Automation
Beyond package managers, other areas can accumulate disk space:
- Temporary files:
/tmpand/var/tmp. Most distributions handle this withsystemd-tmpfiles-clean.service, but manual intervention might be needed on heavily used systems. - Log files:
/var/log/. Log rotation (e.g.,logrotate) is typically configured, but misconfigurations or verbose applications can lead to large log files. - Docker images/volumes/containers: If using Docker,
docker system prune -ais invaluable for clearing unused images, containers, networks, and volumes.
Automation with Cron
For systems with small /var partitions or frequent package installations, automating cleanup can prevent disk space issues. A weekly or monthly cron job is a common practice.
# Example cron job for a Debian/Ubuntu system (/etc/cron.weekly/apt-cleanup)
#!/bin/bash
/usr/bin/apt clean
/usr/bin/apt autoremove --purge -y
/usr/bin/apt autoclean
# Example cron job for a RHEL/CentOS system (/etc/cron.weekly/dnf-cleanup)
#!/bin/bash
/usr/bin/dnf clean all -y
/usr/bin/dnf autoremove -y
# Make the script executable
sudo chmod +x /etc/cron.weekly/apt-cleanup
Ensure these scripts are owned by root and have execute permissions.
Comparison Table: Package Manager Cleanup Commands
| Category | APT (Debian/Ubuntu) | DNF (RHEL/CentOS/Fedora) | APK (Alpine) |
|---|---|---|---|
| Clean Package Cache | apt cleanapt autoclean |
dnf clean all |
apk cache clean |
| Remove Orphaned Dependencies | apt autoremove |
dnf autoremove |
apk autoremove |
| Remove Dependencies + Purge Configs | apt autoremove --purge |
N/A (manual config deletion) | N/A |
| Remove Specific Package (with configs) | apt purge <pkg> |
dnf remove <pkg> (configs remain) |
apk del <pkg> |
| Kernel Cleanup | apt purge linux-image-X linux-headers-X |
dnf remove kernel-core-X kernel-modules-X |
N/A (minimal kernels) |
Common Pitfalls
- Removing the active kernel: Always verify
uname -rbefore purging kernel packages. Removing the currently running kernel or all kernels can render your system unbootable. Keep at least two, preferably three, working kernel versions. - Aggressive log deletion: Deleting files directly from
/var/logwithout understandinglogrotatecan disrupt services or remove forensic data. Uselogrotate -f /etc/logrotate.confto force rotation if logs are excessively large. - Forgetting
--purge: On Debian-based systems, simply usingapt removeleaves configuration files behind. This might be desired for reinstallation, but for full cleanup,apt purgeorapt autoremove --purgeis necessary. - Ignoring non-package manager files: Databases, user data, application caches (e.g., browser caches, Docker images, build artifacts) can consume vast amounts of space and are not managed by package tools. Use tools like
du -sh *to investigate large directories. - Running commands without
sudo: Most cleanup operations require root privileges. Failing to usesudowill result in permission errors and no changes.