How to Remove Duplicate Packages in CentOS/RHEL

Today, I come across a situation where, while patching an RHEL server yum update command got interrupted and started showing errors on the screen as shown below.

error: Couldn't fork %post(ntp-4.2.6p5-12.el6_9.1.x86_64): Cannot allocate memory
Error in POSTIN scriptlet in rpm package ntp-4.2.6p5-12.el6_9.1.x86_64
Updating : apr-devel-1.3.9-5.el6_9.1.x86_64 15/62
Updating : kexec-tools-2.0.0-307.el6_9.1.x86_64 16/62
error: Couldn't fork %post(kexec-tools-2.0.0-307.el6_9.1.x86_64): Cannot allocate memory
Error in POSTIN scriptlet in rpm package kexec-tools-2.0.0-307.el6_9.1.x86_64
Updating : lvm2-2.02.143-12.el6_9.1.x86_64 17/62
error: Couldn't fork %post(lvm2-2.02.143-12.el6_9.1.x86_64): Cannot allocate memory
Error in POSTIN scriptlet in rpm package lvm2-2.02.143-12.el6_9.1.x86_64

After investigation, I come to know that my server’s memory including the swap got fulled. This results in duplicate packages. In this post, we are going to see how to remove these duplicate packages from the rpm database.

1) List down all duplicate packages from the system:

# package-cleanup --dupes
Loaded plugins: product-id, rhnplugin
This system is receiving updates from RHN Classic or RHN Satellite.
autofs-5.0.5-132.el6.x86_64
autofs-5.0.5-133.el6_9.x86_64
lvm2-2.02.143-12.el6_9.1.x86_64
lvm2-2.02.143-12.el6.x86_64
apr-1.3.9-5.el6_9.1.x86_64
apr-1.3.9-5.el6_2.x86_64

–dupes: Scan for duplicates in the local RPM database.

2) How to remove older version of duplicate packages from the system?

# package-cleanup --cleandupes

–cleandupes: Scan for duplicates in the local RPM database and clean out the older versions.

3) How to remove newer version of duplicate packages? With the help of package-cleanup –cleandupes command only older version of duplicate packages can be removed. In order to remove newer version one can use below command:

# rpm -e –justdb –nodeps [Packages-Name]

For example:

# rpm -e --justdb --nodeps autofs-5.0.5-133.el6_9.x86_64

here, –justdb: Update only the database, not the filesystem. –nodeps: Don’t do a dependency check before installing or upgrading a package.

Source: man package-cleanup(1) man RPM(8)