Red Hat / CentOS / Fedora – Fix Yum Duplicates

When nothing else seemed to work this helped me to get rid of all the duplicates from failed yum updates.

yum update glibc -t --skip-broken
package-cleanup --dupes --skip-broken
package-cleanup --cleandupes --skip-broken
updatedb
sudo yum clean packages --skip-broken
sudo yum clean headers --skip-broken
sudo yum clean dbcache --skip-broken
sudo yum clean metadata --skip-broken
sudo yum clean all --skip-broken
sudo rm -rf /var/cache/yum/
sudo mkdir /var/cache/yum/

sudo rm -f /var/lib/rpm/__db*
yum makecache -t --skip-broken

yum distro-sync -t --skip-broken

After trying the above I still had some duplicates on a different server but the ‘Remove Duplicates’ script below seemed to work fine for me.

NOTE: I was looking at having to rebuild one of my servers so I simply tried it as last resort and it worked for me but as always be cautious running in sudo.

rpm -qa --queryformat="%{NAME} %{ARCH}\n" | sort | uniq -c | grep -v " 1 " | cut -c 9- | cut -d" " -f1
# Credit for this script goes to Toracat at https://www.centos.org/forums/viewtopic.php?t=44356
sudo sh -c 'for file in `rpm -qa --queryformat="%{NAME} %{ARCH}\n" | sort | uniq -c | grep -v " 1 " | cut -c 9- | cut -d" " -f1`; do rpm -q --last $file | head -1 | cut -d" " -f1; done | grep -v kernel | grep -v gpg-pubkey | xargs rpm -e --justdb --nodeps'
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.