81

I have installed some rpm package on my Fedora 17. Some packages had a lot of dependencies. I have removed some packages but I forgot remove unused dependencies with yum remove.

How can I do that now?

2

5 Answers 5

131

If you install a package with yum install, say pdftk, it will pull in a lot of dependencies:

Installed:
  pdftk.x86_64 0:1.44-10.fc18

Dependency Installed:
  bouncycastle.noarch 0:1.46-6.fc18     
  itext-core.noarch 0:2.1.7-14.fc18     
  libgcj.x86_64 0:4.7.2-8.fc18          
  bouncycastle-mail.noarch 0:1.46-6.fc18
  java-1.5.0-gcj.x86_64 0:1.5.0.0-40.fc18
  sinjdoc.x86_64 0:0.5-13.fc18
  bouncycastle-tsp.noarch 0:1.46-5.fc18
  java_cup.noarch 1:0.11a-10.fc18
  itext.x86_64 0:2.1.7-14.fc18   
  javamail.noarch 0:1.4.3-12.fc18

Complete!

yum remove pdftk will remove only that package and not all the dependencies.

But you can look at all the 'transactions' (install, remove etc.):

$ sudo yum history list pdftk
ID     | Command line             | Date and time    | Action(s)      | Altered
-------------------------------------------------------------------------------  
    88 | install pdftk            | 2012-12-14 13:35 | Install        |   11   

And then you can undo that transaction:

$ sudo yum history undo 88
Undoing transaction 88, from Fri Dec 14 13:35:34 2012
    Dep-Install bouncycastle-1.46-6.fc18.noarch       @fedora
    Dep-Install bouncycastle-mail-1.46-6.fc18.noarch  @fedora
    Dep-Install bouncycastle-tsp-1.46-5.fc18.noarch   @fedora
    Dep-Install itext-2.1.7-14.fc18.x86_64            @fedora
    Dep-Install itext-core-2.1.7-14.fc18.noarch       @fedora
    Dep-Install java-1.5.0-gcj-1.5.0.0-40.fc18.x86_64 @fedora
    Dep-Install java_cup-1:0.11a-10.fc18.noarch       @fedora
    Dep-Install javamail-1.4.3-12.fc18.noarch         @fedora
    Dep-Install libgcj-4.7.2-8.fc18.x86_64            @fedora
    Install     pdftk-1.44-10.fc18.x86_64             @fedora
    Dep-Install sinjdoc-0.5-13.fc18.x86_64            @fedora
    ...
    Complete!
5
  • 16
    +1 Awesome solution and very easy to do. I've never heard of yum history. Thanks! May 14, 2014 at 20:24
  • 8
    And what if 89 depends on java_cup or libgcj?
    – WernerCD
    May 24, 2016 at 12:28
  • 2
    Shouldn't this be an accepted answer?
    – Celdor
    Mar 7, 2018 at 11:00
  • @WernerCD, in debian dpkg etc comlains that a package can not be removed if another depends on it. Ohe could hope the same happens here.
    – Lenne
    Dec 27, 2019 at 13:52
  • No longer necessary, see some of the other answers...
    – rogerdpack
    Nov 30, 2020 at 18:38
96

Starting from Fedora 18, you can simply use this command

yum autoremove

or

yum remove --setopt=clean_requirements_on_remove=1

You can also apply autoremove command with specific package

yum autoremove <package>

Which will remove unneeded dependencies from that installed package. autoremove is very much an alias of remove --setopt=clean_requirements_on_remove=1 but for some reasons, is still undocumented.

4
  • 5
    command yum remove --setopt=clean_requirements_on_remove=1 works for me in centOS
    – BMW
    Jul 13, 2014 at 9:57
  • 2
    For me the package-cleanup --leaves && yum autoremove removed libvorbis and so made the internet-radio-streaming "Ices" application die -- probably because it isn't in repos and I had to compile it.
    – Nakilon
    Dec 11, 2018 at 20:22
  • 1
    Command yum autoremove works on CentOS 7.6 as well.
    – scrutari
    Feb 1, 2019 at 22:20
  • yum autoremove fails to remove 4 old kernels on CentOS 7 Sep 18, 2019 at 12:51
46

It's not easy. How do you differentiate between "a file that was required by something I have since removed" from "a file that is not required by anything else that I really want"?

You can use the package-cleanup command from the yum-utils package to list "leaf nodes" in your package dependency graph. These are packages that can be removed without affecting anything else:

$ package-cleanup --leaves

This will produce a list of "libraries" on which nothing else depends. In most cases you can safely remove these packages. If you add --all to the command line:

$ package-cleanup --leaves --all

You'll get packages that aren't considered libraries, also, but this list is going to be so long that it probably won't be useful.

2
  • 10
    APT (the Debian equivalent of Yum) has a notion of “automatically installed package”. If a package wasn't explicitly requested but only pulled in as a dependency, it'll be automatically removed (with a confirmation prompt) if the packages that depend on it are all removed. Without an indication of this type, it is indeed not easy. Jun 6, 2012 at 23:53
  • 1
    missed command to remove all packages directly from package-cleanup --leaves
    – brauliobo
    Jan 10, 2020 at 14:43
12

I took larsks answer one step farther.

$ package-cleanup -q --leaves | xargs -l1 yum -y remove 

This grabs all of the dependencies that can be removed without affecting anything else and then removes them. Better then going through one by one.

"-q" is useful on some systems which print "Setting up yum" otherwise, causing this command to remove yum. And that's not what you want.

5
  • 4
    package-cleanup outputs "Setting up yum" on my machine, which resulted in Yum removing itself. I'm now trying to figure out how to sort this out. Feb 1, 2015 at 17:05
  • @PaulLammertsma just add -q option to package-cleanup. This happened to me as well :D
    – Ealhad
    Aug 3, 2015 at 15:23
  • 1
    package-cleanup -q --leaves | xargs -r -l1 yum -y remove will silently skip xargs if package-cleanup output is empty, useful for automated scripts like ansible, chef, puppet, vagrant Oct 5, 2018 at 16:02
  • this is The Answer.
    – xCovelus
    May 1, 2019 at 15:47
  • Because "...removed without affecting anything else..." you should remove the -y so that future users won't get burned by whatever burned @Paul Lammertsma Jul 7, 2021 at 16:22
6

In newer Fedoras with dnf, you can use dnf repoquery --unneeded as a replacement for package-cleanup --leaves.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy