r/archlinux What's the best way to search for unneeded packages?

posted by u/MertsA on 18 Feb 2017

And I don't just mean libraries that used to be dependencies, Fedora has rpmreaper which is perfect for this. What's a good rpmreaper equivalent for Arch?

posted by u/du5tball on 18 Feb 2017 20▲

Either pacman -Qdtq | sudo pacman -Rns - or pacman -Qdttq | sudo pacman -Rns -. The first removes all unneeded packages, but leaves optional dependencies intact, while the second removes optional dependencies as well.

posted by u/boteium on 19 Feb 2017 8▲

That's quite scary ! I ran the first command and pacman says there are 304 package 2G in total needs to be removed.

posted by u/Nibodhika on 19 Feb 2017 3▲

I usually do this, but instead of streaming it directly I throw the result of -Qdtq to a file

pacman -Qdtq > pkgs

Read the file in order to ensure I'm not removing something I need, and then remove all the packages in the file

cat pkgs | pacman -Rns -

posted by u/du5tball on 19 Feb 2017 2▲

It's asking anyway and I could back out. And I usually know already what gets removed.

posted by u/[deleted] on 19 Feb 2017 1▲

How "safe" is running the first command? As in, how big is the probability of breaking something? I'm relatively new to Arch and not entirely 100% confident yet.

posted by u/du5tball on 19 Feb 2017 17▲

Let's disect this:

  • -Q queries the local database
  • d queries for dependencies
  • t queries for unrequired packages
  • q only returns the package name

So it returns all unneeded dependencies (and only their name), and then feeds that into the removal command. The second command with the two ts can (or rather will) potentially remove features from programs, because it also outputs optdepends. But you can't break anything that way.

posted by u/[deleted] on 19 Feb 2017 1▲

Thank you, that explains it perfectly!

posted by u/hoppi_ on 19 Feb 2017 1▲

Thank you for the explanation. :)

But what about the dash after Rns, as in -Rns -? I have not seen that before.

posted by u/du5tball on 19 Feb 2017 5▲

The dash takes the output from the first command. Without it, the output from the first command wouldn't be piped to the second.

posted by u/hoppi_ on 04 Mar 2017 1▲

Thanks :)

posted by u/neverdonebs on 19 Feb 2017 1▲

The dash is a convenient, used by many programs, to read from stdin.

posted by u/lowpass on 19 Feb 2017 1▲

Could do the same thing with xargs couldn't you?

posted by u/SoBFiggis on 20 Feb 2017 1▲

Yes

pacman -Qdtq | xargs sudo pacman -Rns

posted by u/hoppi_ on 18 Apr 2017 1▲

This commands now gives me an error:

Fehler: Keine Ziel-Dateien spezifiziert (benutzen Sie -h für Hilfe)

-> translated ->

Error: No target files specified (use -h for help)

Used it normally like 3-4 weeks ago... hm. *chin-scratching emoji

posted by u/du5tball on 18 Apr 2017 2▲

If there is nothing to remove, it gives an error, because you just ran a pointless command.

posted by u/hoppi_ on 18 Apr 2017 2▲

Oh. Ok :) Thank you.

posted by u/z3ntu on 18 Feb 2017 16▲

I am always using sudo pacman -Rsn $(pacman -Qdtq)

posted by u/lovelybac0n on 19 Feb 2017 3▲

Use this command all the time, no worries for years now.

posted by u/Rostgnom on 19 Feb 2017 2▲

Could you give a quick description of what it does

posted by u/[deleted] on 19 Feb 2017 12▲

pacman -Qdtq = list all installed packages[Q] that are depends[d] and also not required by any package [t], also dont list extra version info [q].

pacman -Rsn = remove this package(s)[R] recursively[s], meaning it will do this all again for unreq packages, and remove confs[n].

so together it lists all unrequired packages and then recursively uninstalls them, which just does the same thing for the orphaned packages.

but man pacman or pacman -Rh & pacman -Qh would tell you this.

posted by u/Trollw00t on 19 Feb 2017 1▲

Just for clarity: It does not remove optional dependencies, which might delete additional features to programs?

Just packages that really aren't needed anymore?

posted by u/[deleted] on 19 Feb 2017 1▲

It keeps them. For them to be removed, you'd need -Qdttq.

posted by u/Trollw00t on 19 Feb 2017 1▲

Thanks mate!

It still wants to delete things like cmake or dkms. Did I install that for nothing or... should I be a bit more cautious?

posted by u/[deleted] on 19 Feb 2017 1▲

You should be. It often removes things you do need, but it doesn't notice it.

posted by u/Trollw00t on 19 Feb 2017 2▲

Ok, now I'm confused. People say this command is safe and doesn't remove dependencies, but now it still does?

So... is there a safe command to delete really unneeded packages or how to approach it?

posted by u/[deleted] on 19 Feb 2017 1▲

It doesn't remove needed depends, but programs marked as depends you may need.

posted by u/hatperigee on 19 Feb 2017 1▲

No, some amount of your involvement is required to make sure that pacman doesn't try to remove packages you actually need.

Pacman can become confused because a package was installed with the "wrong" installation reason. If you notice pacman trying to remove something you need installed, reinstall it as an explicit package and not as a dependency.

posted by u/Trollw00t on 19 Feb 2017 1▲

Aaaaaah, so for example:

I get the pacman -Qdtq and it would list and remove cmake - but I need it.

So I pacman -S cmake and on the next pacman -Qdtq it wouldn't be listed?

EDIT: Tested it quickly myself. Yes, this works. :)

So I'll now pacman -Qdtq, look which packages I'd need. Uninstall them and reinstall needed ones everytime?

Continue ->
posted by u/Worworen on 19 Feb 2017 1▲

Maybe you don't need them?

posted by u/Trollw00t on 19 Feb 2017 1▲

Shouldn't I need them when using the AUR?

Also erm... what if I'll miss them? :(

posted by u/s0r00t on 19 Feb 2017 1▲

Install them again :D

posted by u/Trollw00t on 19 Feb 2017 1▲

Obvious solution. :D

Will yaourt/the-build-process ask for given packages or will it error out and I have to see what's missing?

Because if it's asking anyway... trash them packages!

Continue ->
posted by u/synthequated on 19 Feb 2017 3▲

The arch wiki has a few commands that you might find interesting for package cleanup.

posted by u/[deleted] on 19 Feb 2017 3▲

pkgbrowser (AUR) is a graphical tool that lets you see what packages you have installed, if they're orphans, theire dependecies, and so on

posted by u/MertsA on 19 Feb 2017 2▲

Thanks, this is very close to what I was hoping for. This plus just using pacman to do the actual uninstall works great.

posted by u/unworldlyalex on 19 Feb 2017 0▲

Why would I build a tool from the AUR when I can run one command and it's done?

posted by u/[deleted] on 19 Feb 2017 3▲

for the GUI

posted by u/MertsA on 19 Feb 2017 2▲

Because the dependency graph is complex and this is a perfect case where just a plain text list of packages in response to a single query just doesn't cut it. If you're ever using a RHEL based distro try rpmreaper and you'll see exactly what I mean. It's hard to display things like circular dependency chains using just pacman.

posted by u/severach on 20 Feb 2017 1▲

I just cleaned up a bunch of packages with pacaur -Syau. I didn't finish the command. I just looked through the list and removed most of what it says "is not present in AUR." I figured these were packages that were removed from the repos and not placed on the AUR.

posted by u/Vegetable-Ad-8224 on 30 May 2022 1▲

true qdtq i get it but i installed a shit load of dependecies? how about those

posted by u/bri-an on 18 Feb 2017 -8▲

What do you consider to be "unneeded"? On one extreme, you don't need anything except what comes in base. You can uninstall everything outside of base like this:

$ comm -23 <(pacman -Qqt | sort) <(pacman -Qgq base | sort) | sudo pacman -Rns -

On the other extreme, maybe you need everything that you explicitly installed (and their dependencies), but not orphans. You can unstall those like this:

$ pacman -Qqdt | sudo pacman -Rns -

For any in-between case, you're going to have to intervene: pacman doesn't "know" what's needed or unneeded, because there's no strict definition of "unneeded". Your best bet is to study man pacman and pacman tips and tricks to learn how to query your packages. For example, at some point you probably want to list all explicitly installed packages outside of base, base-devel, etc. and cherry-pick what you want to uninstall, e.g. programs you no longer use.

posted by u/MertsA on 18 Feb 2017 3▲

Well specifically a faster way to cull old packages that were explicitly installed. Also, it needs to cover circular dependencies and not just show leaf nodes. What I was hoping for was some graphical or curses based UI to show the package graph. rpmreaper works amazing on RHEL based distros and I already manually cleaned up a bunch of unneeded packages using pacman but just having a plaintext list of packages to work with is a major pain. I figured surely I must be missing something, I guess I'm just spoiled.

posted by u/[deleted] on 18 Feb 2017 1▲

[deleted]

posted by u/DarkShadow4444 on 19 Feb 2017 2▲

On one extreme, you don't need anything except what comes in base.

Not quite right, I need grub or my PC won't boot.

posted by u/artisebrown on 19 Feb 2017 1▲

Systemd-boot?

posted by u/DarkShadow4444 on 19 Feb 2017 1▲

AFAIK grub is better suited for legacy BIOS booting.

posted by u/MertsA on 20 Feb 2017 1▲

That's not true, if it's a vm you really don't need anything at all other than base. You can tell QEMU to boot a kernel from the host and just give it the root device of the guest.

posted by u/DarkShadow4444 on 20 Feb 2017 1▲

Not talking about VMs though.

posted by u/MertsA on 20 Feb 2017 1▲

He's still right though. Even some of the stuff in base isn't strictly necessary.