Removing Duplicate Packages from a Failed Yum Transaction

Environment

  • CentOS 8 - 7 + Older versions
  • Red Hat Enterprise Linux 8 - 7 + Older RHEL versions

Issue

  • How to remove older versions of a package that has multiple versions installed on CentOS?
  • After a failed yum transaction, yum-complete-transaction fails to complete, and the duplicates are still present.
  • Yum fails with the message <package_name-version1> is a duplicate with <package_name-version2>.
  • Running package-cleanup --cleandupes results in the error Error: Depsolving loop limit reached.

Resolution

Try to Complete the Transaction

Note: This option only applies to CentOS 5, 6, 7

  1. Before manually removing the duplicates, confirm if yum-complete-transaction will resolve the issue.
    yum-complete-transaction
    
  2. If the completion is successful and there are no further duplicates, you can clear the message using the --cleanup-only option.
    yum-complete-transaction --cleanup-only
    

Manually Remove Duplicates

The following steps involve backing up the rpm database and manually removing the duplicate packages.

CentOS 5

  • CentOS 5 does not have yum check, so an alternate method is used.
    tar cjf /tmp/rpm_db.tar.bz2 /var/lib/{rpm,yum}
    package-cleanup --dupes &> /tmp/duplicate
    awk 'NR%2==0' /tmp/duplicate > /tmp/remove
    cat /tmp/remove | egrep -v ":" > /tmp/remove1
    cat /tmp/remove | egrep ":" | awk -F':' '{ print $NF }' >> /tmp/remove1
    for i in $(cat /tmp/remove1); do rpm -e --justdb --nodeps $i; done
    yum update
    

CentOS 6, 7

  • yum check is used to identify duplicates.
    tar cjf /tmp/rpm_db.tar.bz2 /var/lib/{rpm,yum}
    yum check &> /tmp/yumcheck
    grep "duplicate" /tmp/yumcheck | awk '{ print $NF }' | egrep -v ":" > /tmp/duplicaterpms
    grep "duplicate" /tmp/yumcheck | awk '{ print $NF }' | egrep ":" | awk -F':' '{ print $NF }' >> /tmp/duplicaterpms
    for i in $(cat /tmp/duplicaterpms); do rpm -e --justdb --nodeps $i; done
    yum update
    

CentOS 8

  • For RHEL 8, the backup includes /var/lib/dnf and /etc/dnf*.
    tar cjf /tmp/rpm_db.tar.bz2 /var/lib/{rpm,dnf} /etc/dnf*
    repoquery --duplicated --latest-limit=-1 -q &> /tmp/duplicateolder
    for i in $(cat /tmp/duplicateolder); do rpm -ev --justdb --nodeps $i; done
    yum update
    

After Removing the Duplicates

  • If a new kernel was part of the failed transaction, you may need to reinstall it to generate correct grub entries and initrd/initramfs.
    yum reinstall kernel-<version>-<release>.<arch>
    

Identify Remaining RPM Corruption

  • To identify any remaining RPM corruption, use rpm -Va

to verify files provided by RPMs against the installed RPMs.

prelink -ua; rpm -Va &> /tmp/rpm_Va.out
  • Exclude configuration files from the output to identify a list of packages to reinstall.
    awk '$2 !~/^[cg]$/ {print $NF}' /tmp/rpm_Va.out | xargs rpm -qf 2>/dev/null | sort -u |& tee /tmp/pkgs.out
    
  • Reinstall the packages to replace any files that don’t match the installed RPMs.
    yum reinstall $(cat /tmp/pkgs.out)
    

Root Cause

The issue occurs when a yum update is interrupted, leaving the old versions of packages installed in addition to the new ones. These duplicates cause confusion in yum’s logic and can lead to errors like “protected multilib” or misleading dependency errors.

Diagnostic Steps

  • Identify any duplicates on the system.
    package-cleanup --dupes
    yum check
    
  • For CentOS 6 and newer, check the yum history to look for failed transactions marked by a *.
    yum history
    
  • Use rpm -Va to verify RPM vs. file integrity, and refer to the rpm man page for interpreting the output.

Note: The provided steps aim to resolve issues related to duplicate packages resulting from failed yum transactions.