Ora

How do I delete a package in RHEL?

Published in RHEL Package Management 5 mins read

To delete a package in RHEL, you primarily use the dnf or yum package manager, depending on your RHEL version. These commands allow you to remove individual packages, multiple packages, orphaned dependencies, or even entire package groups efficiently.

Understanding Package Managers: DNF vs. YUM

Red Hat Enterprise Linux (RHEL) uses different package managers depending on the version:

  • DNF (Dandified YUM): This is the default package manager for RHEL 8 and newer. DNF is the next-generation version of YUM, offering better performance, dependency resolution, and a more robust API.
  • YUM (Yellowdog Updater, Modified): This was the default package manager for RHEL 7 and older. While DNF is the successor, yum commands still often work as an alias to dnf on RHEL 8+, ensuring backward compatibility.

In most cases, you can use sudo before your package manager commands to execute them with root privileges, which is required for package modifications.

Removing Individual Packages

To remove a single package, use the dnf remove or yum remove command followed by the package name.

Syntax:

sudo dnf remove <package_name>
sudo yum remove <package_name>

Example:

Let's say you want to remove the httpd web server package:

sudo dnf remove httpd

or for older RHEL versions:

sudo yum remove httpd

When you run this command, the package manager will list the package(s) to be removed, along with any dependencies that will also be removed because no other installed package requires them. You'll be prompted to confirm the action by typing y or yes.

  • Non-interactive Removal: To bypass the confirmation prompt, you can add the -y option:
    sudo dnf remove -y httpd

Removing Multiple Packages

You can remove several packages at once by listing them space-separated after the remove command.

Syntax:

sudo dnf remove <package1> <package2> <package3>
sudo yum remove <package1> <package2> <package3>

Example:

To remove both nginx and php packages:

sudo dnf remove nginx php

Removing Orphaned Dependencies (Autoremove)

When you remove a package, its dependencies might remain on your system even if no other installed package needs them. These are known as "orphaned dependencies." dnf and yum can automatically remove these unneeded packages using the autoremove command.

Syntax:

sudo dnf autoremove
sudo yum autoremove

Example:

sudo dnf autoremove

This command will list all packages that were installed as dependencies but are no longer required by any currently installed software. It's a good practice to run this periodically to keep your system clean.

Deleting Package Groups (Using yum and dnf)

Sometimes, packages are installed as part of a larger group (e.g., "Development Tools" or "Server with GUI"). You can remove an entire package group using specific commands.

First, you might want to list available groups to identify the exact name or ID:

sudo dnf group list
sudo yum group list

By Group Name

To remove a package group by its name, use the group remove command or the @ prefix with remove.

Syntax:

sudo yum group remove "<group_name>"
sudo yum remove @"<group_name>"

The dnf equivalent works similarly:

sudo dnf group remove "<group_name>"
sudo dnf remove @"<group_name>"

Example:

To remove the "Development Tools" group (ensure you use quotes if the group name contains spaces):

sudo yum group remove "Development Tools"

Alternatively, using the @ prefix:

sudo yum remove @"Development Tools"

For RHEL 8 and newer, the dnf commands would be:

sudo dnf group remove "Development Tools"

or

sudo dnf remove @"Development Tools"

By Group ID

If you prefer, you can also remove a package group using its unique Group ID. You can find the Group ID by running sudo yum group list or sudo dnf group list and looking for the ID in parentheses.

Syntax:

sudo yum group remove <group_ID>
sudo dnf group remove <group_ID>

Example:

If "Development Tools" had a Group ID of 123:

sudo yum group remove 123

For RHEL 8 and newer:

sudo dnf group remove 123

Important Considerations Before Deletion

  • Dependency Impact: Always review the list of packages dnf or yum proposes to remove. Deleting a critical system package or a package required by other essential applications can lead to system instability or non-functional services.
  • Checking Dependencies: Before removing, you can check what packages depend on the one you intend to remove using dnf repoquery --whatrequires <package_name> or yum deplist <package_name>.
  • Dry Run (DNF): For DNF, you can often perform a "dry run" using --assumeno to see what would happen without actually making changes:
    sudo dnf remove <package_name> --assumeno

Summary of Package Removal Commands

Here's a quick reference for common package removal operations:

Command Description RHEL Version
sudo dnf remove <package_name> Removes a specific package and its unneeded dependencies. RHEL 8+
sudo yum remove <package_name> Removes a specific package and its unneeded dependencies. RHEL 7 & 8+
sudo dnf remove <p1> <p2> Removes multiple specific packages. RHEL 8+
sudo yum remove <p1> <p2> Removes multiple specific packages. RHEL 7 & 8+
sudo dnf autoremove Removes all orphaned dependencies from the system. RHEL 8+
sudo yum autoremove Removes all orphaned dependencies from the system. RHEL 7 & 8+
sudo dnf group remove "Group Name" Removes all packages belonging to a specified group by name. RHEL 8+
sudo dnf remove @"Group Name" Removes all packages belonging to a specified group by name (alternative). RHEL 8+
sudo dnf group remove <group_ID> Removes all packages belonging to a specified group by ID. RHEL 8+
sudo yum group remove "Group Name" Removes all packages belonging to a specified group by name. RHEL 7 & 8+
sudo yum remove @"Group Name" Removes all packages belonging to a specified group by name (alternative). RHEL 7 & 8+
sudo yum group remove <group_ID> Removes all packages belonging to a specified group by ID. RHEL 7 & 8+

For more detailed information, consult the official Red Hat documentation for DNF and YUM.