3

I'm trying to remove a bunch of packages that have 'abc' in it. However, I don't want to remove 'abc-def'. How can I accomplish this?

For example, how to remove every packages that have 'php' in it, except 'php-common'?

I tried adding --exclude to the command, but it doesn't work as expected - 'php-common' was still in the deletation list.

# yum --exclude=php-common remove *php*
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package php-cli.x86_64 0:5.4.16-36.el7_1 will be erased
---> Package php-common.x86_64 0:5.4.16-36.el7_1 will be erased
---> Package php-fpm.x86_64 0:5.4.16-36.el7_1 will be erased
---> Package php-mcrypt.x86_64 0:5.4.16-3.el7 will be erased
---> Package php-mysqlnd.x86_64 0:5.4.16-36.el7_1 will be erased
---> Package php-pdo.x86_64 0:5.4.16-36.el7_1 will be erased
--> Finished Dependency Resolution

yum remove *php* !php-common,--exclude=php-common* and --exclude=php-common.x86_64 0:5.4.16-36.el7_1 doesn't work either.

I'm asking this because I have a huge package (~1.5 GB) that I still need and don't want to re-download it because my internet is slow.

OS: CentOS 7.

3 Answers 3

9

Simply prefix the package name that you want to keep with -. You need to add -- before the list of packages to ensure that the name of the package is not treated like an option:

yum remove *php* -- -php-common 

From the man page (under the install option):

If the name starts with a - character, then a search is done within the transaction and any matches are removed.

1
  • Neat, but I needed to reverse it to yum remove *php* -- -php-common.
    – Sven
    Nov 19, 2015 at 9:23
5

--exclude will exclude a package from the repositories, as it wasn't available to install, but it doesn't act on installed packages.

One way to really protect your package from deinstallation is to put its name into protected_packages in /etc/yum.conf, but then yum wouldn't process a wildcard that also matches that package.

One solution:

With your specific example php-common, this will not really work as the dependency resolver will interfere, but if dependencies are not a problem, you can try the following:

yum erase `yum list installed | cut -d " " -f 1  | grep php | grep -v php-common `

The subcommand will generate a list of installed packages, cut it to the first column, reduce it to packages containing php in the name and then eliminates php-common from the list.

1

╮(╯_╰)╭
I wrote a python to resolve this problem yesterday.
I hope it is useful for you and who was troubled about this problem. : - )

I noted down the development process on my blog
http://blog.j3l11234.com/2016/11/23/yum-remove-with-exclude/

here is the usage:

# ./remove.py -e *php* -x php-common
analyzing exclude packages and their dependencies ...

analyzing packages which will be removed...

Loaded plugins: fastestmirror, langpacks
Resolving Dependencies
--> Running transaction check
---> Package php-cli.x86_64 0:7.0.12-2.el7.remi will be erased
---> Package php-fpm.x86_64 0:7.0.12-2.el7.remi will be erased
---> Package php-gd.x86_64 0:7.0.12-2.el7.remi will be erased
---> Package php-mbstring.x86_64 0:7.0.12-2.el7.remi will be erased
---> Package php-mysqlnd.x86_64 0:7.0.12-2.el7.remi will be erased
---> Package php-opcache.x86_64 0:7.0.12-2.el7.remi will be erased
---> Package php-pdo.x86_64 0:7.0.12-2.el7.remi will be erased
---> Package php-xml.x86_64 0:7.0.12-2.el7.remi will be erased
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================
 Package            Arch         Version                  Repository                                     Size
==============================================================================================================
Removing:
 php-cli            x86_64       7.0.12-2.el7.remi        @/php-cli-7.0.12-2.el7.remi.x86_64             12 M
 php-fpm            x86_64       7.0.12-2.el7.remi        @/php-fpm-7.0.12-2.el7.remi.x86_64            4.1 M
 php-gd             x86_64       7.0.12-2.el7.remi        @/php-gd-7.0.12-2.el7.remi.x86_64             203 k
 php-mbstring       x86_64       7.0.12-2.el7.remi        @/php-mbstring-7.0.12-2.el7.remi.x86_64       2.8 M
 php-mysqlnd        x86_64       7.0.12-2.el7.remi        @/php-mysqlnd-7.0.12-2.el7.remi.x86_64        868 k
 php-opcache        x86_64       7.0.12-2.el7.remi        @/php-opcache-7.0.12-2.el7.remi.x86_64        396 k
 php-pdo            x86_64       7.0.12-2.el7.remi        @/php-pdo-7.0.12-2.el7.remi.x86_64            381 k
 php-xml            x86_64       7.0.12-2.el7.remi        @/php-xml-7.0.12-2.el7.remi.x86_64            854 k

Transaction Summary
==============================================================================================================
Remove  8 Packages

Installed size: 22 M
Is this ok [y/N]: 

download link

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .