How to Safely Clear YUM Cache Without Affecting RPM Package Database


4 views

Many Linux administrators assume yum clean all completely purges cached packages, but in reality it leaves certain artifacts behind. The package cache in /var/cache/yum can grow significantly over time, especially on systems with frequent updates.

Contrary to initial assumptions, the RPM package database resides in /var/lib/rpm, completely separate from YUM's cache. This explains why operations like rpm -qa continue working even after clearing YUM cache.

# Verify RPM database location
$ rpm --eval '%_dbpath'
/var/lib/rpm

For comprehensive cache cleaning without affecting system state:

# Standard YUM cache cleaning (preserves metadata)
$ sudo yum clean all

# Nuclear option - completely rebuild YUM cache
$ sudo rm -rf /var/cache/yum/*
$ sudo yum makecache

For systems with severe space constraints, these directories are safe to purge:

# Remove downloaded packages (will be re-downloaded when needed)
$ sudo rm -rf /var/cache/yum/x86_64/*/packages/

# Clear old metadata
$ sudo rm -rf /var/cache/yum/x86_64/*/cache/

Always verify system integrity after cache operations:

# Check RPM database consistency
$ sudo rpm -Va

# Verify package queries still work
$ rpm -qa | wc -l

# Test YUM functionality
$ sudo yum list updates

For regular maintenance, consider this cron job:

# Weekly YUM cache cleanup
0 3 * * 0 root /usr/bin/yum clean all >/dev/null 2>&1

Remember that while /var/cache/yum is safe to clear, /var/lib/yum contains important system data that should never be manually modified.


Many administrators assume yum clean all completely clears the package cache, but this isn't entirely accurate. The YUM cache in /var/cache/yum contains several types of data:

/var/cache/yum/
├── x86_64/
│   ├── 7/
│   │   ├── base/          # Repository metadata
│   │   ├── extras/        # Downloaded packages
│   │   └── updates/
├── timedhosts.txt         # Mirror timing data
└── transaction-all/       # Transaction history

To clear downloaded packages while preserving system state:

# Remove downloaded package files (.rpm) only
find /var/cache/yum -name '*.rpm' -exec rm -f {} \;

# Alternative: Clear all cached data except system state
yum clean all
rm -rf /var/cache/yum/*/packages/

Never touch these directories as they contain system state information:

  • /var/lib/rpm/ - RPM database (critical for rpm -qa)
  • /var/lib/yum/ - YUM transaction history
  • /var/cache/yum/*/repodata/ - Repository metadata

For regular maintenance, consider this safe cleanup script:

#!/bin/bash
# Safe YUM cache cleaner
echo "Cleaning YUM cache..."
yum clean all

# Remove orphaned package files older than 30 days
find /var/cache/yum -name '*.rpm' -mtime +30 -exec rm -f {} \;

# Preserve repository metadata
echo "Preserving repository metadata..."
find /var/cache/yum -name 'repodata' -prune -o -exec rm -rf {} \; 2>/dev/null

echo "Cache cleanup complete. Current disk usage:"
du -sh /var/cache/yum/

After cleanup, always verify your system state:

# Check RPM database integrity
rpm -qa | wc -l
rpm --verify

# Test YUM functionality
yum check-update

Only in extreme disk space situations should you consider:

# Complete YUM cache reset (will require redownloading metadata)
rm -rf /var/cache/yum/*
yum makecache

Remember that this will force YUM to redownload all repository metadata during the next operation.