How to Completely Remove Packages with Config Files in CentOS/RHEL (YUM Equivalent of apt-get purge)


1 views

When migrating from Debian/Ubuntu to CentOS/RHEL systems, one notable difference is package cleanup behavior. While apt-get purge removes both packages and configuration files, yum remove intentionally preserves configuration files by design.

The closest native YUM equivalent would be:

yum remove package_name
rm -rf /etc/packagename/

However, this requires manual tracking of configuration locations. A more systematic approach:

First, identify all configuration files associated with a package:

rpm -qc package_name

Example output for httpd:

/etc/httpd/conf/httpd.conf
/etc/httpd/conf.modules.d/00-base.conf
/etc/sysconfig/httpd

Combine these commands for a purge-like operation:

# Remove package
yum remove package_name

# Remove config files
rpm -ql package_name | grep -E "/etc/|/var/" | xargs rm -rf

# Clean up orphaned dependencies
package-cleanup --quiet --leaves | xargs yum remove -y

If using CentOS 8+ or RHEL 8+ with dnf:

dnf remove --remove-leaves package_name
  • Always back up important configuration files before removal
  • Be cautious with wildcard deletions in system directories
  • Some applications may store configurations in unexpected locations (check /var/lib/)

For frequent purges, consider creating a custom RPM that handles cleanup during removal:

%postun
if [ $1 -eq 0 ]; then
    rm -rf /etc/yourapp/
    rm -rf /var/lib/yourapp/
fi

Coming from Debian/Ubuntu to RHEL/CentOS, many admins miss the apt-get purge functionality when working with yum. While yum remove removes the package, it intentionally keeps configuration files in place - a design difference from Debian's approach.

For a true purge equivalent, we need to use rpm directly:


# Remove package and all config files
rpm -e --nodeps package_name
find / -name "*package_name*" -exec rm -rf {} \;

However, this brute-force method isn't recommended as it might remove files that belong to other packages.

The safest method is using yum-utils which provides enhanced package management functions:


# Install yum-utils if not present
yum install yum-utils

# Completely remove package and configs
package-cleanup --leaves | xargs yum remove -y

Let's walk through a real-world scenario of purging MariaDB completely:


# First remove normally
yum remove mariadb-server

# Then clean up residual files
rm -rf /var/lib/mysql
rm -rf /etc/my.cnf
rm -rf /etc/my.cnf.d

For more thorough cleanup, create a custom script:


#!/bin/bash
PKG=$1
yum remove -y $PKG
find /etc -name "*$PKG*" -exec rm -rf {} \;
find /var/lib -name "*$PKG*" -exec rm -rf {} \;
find /var/log -name "*$PKG*" -exec rm -rf {} \;

Before purging, consider backing up configs:


# Create timestamped backup
mkdir -p /root/backups/$(date +%Y%m%d)
find /etc -name "*package_name*" -exec cp -a {} /root/backups/$(date +%Y%m%d) \;

Sometimes a cleaner approach is to rebuild the server or use containers:


# Using Docker for clean environments
docker run -it centos:7
yum install package_name