How To Remove a DNF or Yum Repository

By | 2019-11-03

With both DNF and YUM, there are three ways to remove repositories. One of the methods is specific to RHEL. If you are on a system that doesn’t have the dnf command, you can use the yum command instead.

Using subscription-manager

The subscription-manager program is specific to RHEL and specific to repositories maintained by Red Hat, so you need to use one of the other methods to disable third-party repositories.

To use subscription-manager to remove a repository, follow the example below:

# subscription-manager repos --disable=rhel-7-server-rpms
Repository 'rhel-7-server-rpms' is disabled for this system.

Replace the highlighted portion with the repository you wish to remove.

If you make a mistake and wish to re-enable a repository, follow this example:

# subscription-manager repos --enable=rhel-7-server-rpms
Repository 'rhel-7-server-rpms' is enabled for this system.

Again, replace the highlighted portion with the applicable repo ID.

Editing or Deleting Files in /etc/yum.repos.d

In some cases, you need to edit, rename, or remove files in /etc/yum.repos.d to disable repositories. Repositories are configure with files in the aforementioned directory ending with .repo. Files ending with anything else are ignored. An easy and common way to disable a repository is to rename the file it is defined in.

# pwd
/etc/yum.repos.d
# ls
CentOS-AppStream.repo   CentOS-Debuginfo.repo  CentOS-PowerTools.repo
CentOS-Base.repo        CentOS-Extras.repo     CentOS-Sources.repo
CentOS-centosplus.repo  CentOS-fasttrack.repo  CentOS-Vault.repo
CentOS-CR.repo          CentOS-Media.repo      zabbix.repo
# dnf repolist
Last metadata expiration check: 1:38:22 ago on Thu 24 Oct 2019 01:56:13 PM EDT.
repo id               repo name                                           status
AppStream             CentOS-8 - AppStream                                4,679
BaseOS                CentOS-8 - Base                                     1,655
PowerTools            CentOS-8 - PowerTools                               1,443
centosplus            CentOS-8 - Plus                                        26
extras                CentOS-8 - Extras                                       3
fasttrack             CentOS-8 - fasttrack                                    0
zabbix                Zabbix Official Repository - x86_64                    33
zabbix-non-supported  Zabbix Official Repository non-supported - x86_64       1
# mv CentOS-centosplus.repo CentOS-centosplus.repo.disabled
# dnf repolist
Last metadata expiration check: 1:38:35 ago on Thu 24 Oct 2019 01:56:13 PM EDT.
repo id               repo name                                           status
AppStream             CentOS-8 - AppStream                                4,679
BaseOS                CentOS-8 - Base                                     1,655
PowerTools            CentOS-8 - PowerTools                               1,443
extras                CentOS-8 - Extras                                       3
fasttrack             CentOS-8 - fasttrack                                    0
zabbix                Zabbix Official Repository - x86_64                    33
zabbix-non-supported  Zabbix Official Repository non-supported - x86_64       1

Notice how the repository centosplus is no longer showing. Deleting the file will have the same effect.

You can also edit the files to disable a repository. The file, /etc/yum.repos.d/CentOS-centosplus.repo on a CentOS 8 machine as the following contents:

# CentOS-centosplus.repo

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
#baseurl=http://mirror.centos.org/$contentdir/$releasever/centosplus/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

If you wish to disable this repository, change the line enabled=1 to enabled=0:

# CentOS-centosplus.repo

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
#baseurl=http://mirror.centos.org/$contentdir/$releasever/centosplus/$basearch/os/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

Changing Variable Values

I have seen systems, such as Oracle Linux, that use YUM variables to enable and disable repositories. I modified one of the CentOS repository files to demonstrate:

# CentOS-centosplus.repo

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
#baseurl=http://mirror.centos.org/$contentdir/$releasever/centosplus/$basearch/os/
gpgcheck=1
enabled=$plus
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

Yum variables are set in /etc/yum/vars. Each variable is set by editing the contents of a text file with the corresponding name. In my example, I use the variable plus to control whether or not this repository is enabled. To disable the repository, set it to 0.

# cd /etc/yum/vars
# pwd
/etc/yum/vars
# ls
contentdir  infra  plus
# cat plus 
1
# vi plus 
# cat plus 
0
# dnf repolist
Last metadata expiration check: 1:38:35 ago on Thu 24 Oct 2019 01:56:13 PM EDT.
repo id               repo name                                           status
AppStream             CentOS-8 - AppStream                                4,679
BaseOS                CentOS-8 - Base                                     1,655
PowerTools            CentOS-8 - PowerTools                               1,443
extras                CentOS-8 - Extras                                       3
fasttrack             CentOS-8 - fasttrack                                    0
zabbix                Zabbix Official Repository - x86_64                    33
zabbix-non-supported  Zabbix Official Repository non-supported - x86_64       1

Removing Repository Packages

It is fairly common for application developers to distribute their software in their own repositories. The repository maintainers often provide packages that setup the repositories for you. If this is the case, you can usually just remove that package. The Zabbix monitoring system is a good example of this.

# dnf repolist
Last metadata expiration check: 0:00:54 ago on Thu 24 Oct 2019 10:53:05 AM EDT.
repo id               repo name                                           status
AppStream             CentOS-8 - AppStream                                4,679
BaseOS                CentOS-8 - Base                                     1,655
PowerTools            CentOS-8 - PowerTools                               1,443
centosplus            CentOS-8 - Plus                                        26
extras                CentOS-8 - Extras                                       3
fasttrack             CentOS-8 - fasttrack                                    0
zabbix                Zabbix Official Repository - x86_64                    33
zabbix-non-supported  Zabbix Official Repository non-supported - x86_64       1

Find the package name using rpm -qa:

# rpm -qa 'zabbix*'
zabbix-agent-4.4.1-1.el8.x86_64
zabbix-release-4.4-1.el8.noarch

Remove the package:

# dnf remove zabbix-release

Now the repository is gone:

# dnf repolist
Last metadata expiration check: 0:54:25 ago on Thu 24 Oct 2019 10:53:02 AM EDT.
repo id                        repo name                                  status
AppStream                      CentOS-8 - AppStream                       4,679
BaseOS                         CentOS-8 - Base                            1,655
PowerTools                     CentOS-8 - PowerTools                      1,443
centosplus                     CentOS-8 - Plus                               26
extras                         CentOS-8 - Extras                              3
fasttrack                      CentOS-8 - fasttrack                           0

References