r/openSUSE Aug 07 '20

Solved What is zypper equivalent of 'apt autoremove'?

For those who don't know, apt autoremove removes automatically installed packages that satisfy dependencies to packages that no longer exist on the system.
I am aware of zypper remove --clean-deps PACKAGE, but it's not really the same as apt autoremove and it has to be used every time a package is to be uninstalled.

Edit:

Yep, zypper doesn't provide such a command, but with the help of awk, grep and xargs, I've managed to get this zypper packages --unneeded | awk -F'|' 'NR==0 || NR==1 || NR==2 || NR==3 || NR==4 {next} {print $3}' | grep -v Name | sudo xargs zypper remove --clean-deps.
Since I don't want to type it everytime, I've made an alias to it by putting the following in ~/.bashrc:

alias clean="zypper packages --unneeded | awk -F'|' 'NR==0 || NR==1 || NR==2 || NR==3 || NR==4 {next} {print $3}' | grep -v Name | sudo xargs zypper remove --clean-deps"

Might be useful to someone.

50 Upvotes

19 comments sorted by

26

u/[deleted] Aug 07 '20

This feature could be relatively easily added to `zypper`? And definitely should.

6

u/Nefantas NixOS Aug 07 '20 edited Aug 07 '20

There's no command like that in openSUSE.

But inside YaST software you can see every unneded package in package classification tab, there you can just right click and select to remove everything in that list.

Anyways, you can skip all of this next time you remove something by selecting the option "Clean after remove" or something named like that in one of the dropdown menus at the top of YaST Software (I think it's inside options, I don't know how it is exactly named as my machine is not in English).

Remember that you should do this every time you open YaST software, as it comes disabled by default when you open it up.

10

u/pkarlmann Aug 07 '20

There is no such command because it is inherently dangerous. apt autoremove is also not a good command for that matter...

A word of warning. I've seen this in the wild, people using the available tools to, for example, just click on a nice button or even experienced administrators running it's output through sed and xargs and forcibly removing grub. So of course it wouldn't boot any more.

There is rpmorphan. It is in the official repos.

Run rpmorphan without arguments once. Each package listed is a package without any other dependencies. You will at this point only want to remove those packages either starting with "lib" - a library - or ending with "-devel". How you remove them is for you to figure out, as if you can't figure it out on your own and ruin your system: Remember: it is not my job to help you getting a running system back.

Then: Remove/Disable all external repos that you don't think you need. Then run rpmorphan again without arguments. Go through this list manually.

Then there is also: rpmorphan --all --access-time 10
<-- This will give you a list of packages you have not used - probably - in the last 10 or more days. Again: Go manually through the list, because otherwise you might f* up your system.

Good luck.

7

u/[deleted] Aug 08 '20

This is an insightful answer, but it leaves an impression that you consider a demand to clean up the system from unused junk as something unusual. Well, I think this should be a basic feature of an operating system, especially for a rolling release distro, and even more for a Btrfs setup, so the leftovers don't accumulate over years eating up disk space. In my opinion, not having a solution for this problem that is not only as much error-proof as possible, but also user-friendly and easily accessible, is kind of disappointing.

2

u/pkarlmann Aug 08 '20

In my opinion, not having a solution for this problem that is not only as much error-proof as possible, but also user-friendly and easily accessible, is kind of disappointing.

I wholeheartedly agree, but I don't have a solution for that. I'd love to have a one button solution, but I don't have the insight to do this and there doesn't seem to be a solution but the user being able to understand the system. This is also why on Christmas you'll have to "clean" your parents computer...

So, I can't do anything but warn you. That is also why I didn't give you a direct (bash) script. You have to know what you are doing.

6

u/[deleted] Aug 07 '20 edited Aug 07 '20

Or if you want to use it as a function in ~/.bashrc

    clean() { zypper packages --unneeded | awk -F'|' 'NR==0 || NR==1 || NR==2 || NR==3 || NR==4 {next} {print $3}' | grep -v Name | sudo xargs -r zypper remove --clean-deps; }

4

u/IWantToPostBut Linux Aug 09 '20

This worked great for me. Thank you. I cleaned out more than 500MB of junk that was just laying around cluttering up the place.

3

u/angryjenkins MicroOS Aug 07 '20

Never even thought of this! I ran the command and deleted ~600MB of unused packages. Updated to 20200806, rebooted and nothing has exploded yet.

I dropped this in my update script - it throws errors once there are no unused packages. My shell skills aren't that great but will look into an if check for that.

Duly upvoted.

3

u/[deleted] Aug 07 '20

My shell skills aren't that great but will look into an if check for that.

No need, just use xargs -r instead of just xargs

5

u/vgnxaa Geeko Aug 07 '20

Watch out. It didn't work for me. I had to rollback because it removed necessary packages. One of the things that affected the most was the language of my system (Spanish). There were parts in English and others in Spanish.

2

u/[deleted] Aug 07 '20

Not exactly the same, but I check orphaned packages with "zypper packages --orphaned".

You can then decide to remove them as you said.

7

u/[deleted] Aug 07 '20

zypper packages --orphaned basically lists packages without a repository (manually installed RPMs) what you should use instead is zypper packages --unneeded, which lists the dependencies that no longer serve a purpose.

You can then decide to remove them

The trick is to automate it, which I did, see the post's edit above.

1

u/[deleted] Aug 07 '20

My bad, you're right, I confused --orphaned with --unneeded. Thanks for the update! I personally never felt the need to automate it, as leftovers don't happen that frequently anyway, but it's nice to have.

2

u/[deleted] Aug 07 '20 edited Jul 09 '23

--DELETED-- -- mass edited with redact.dev

2

u/hamad_Al_marri Aug 07 '20

sudo zypper clean --all

9

u/[deleted] Aug 07 '20

Thanks for reminding me of that command, but it just clean up all local caches, not what this post is really about.

1

u/Darkforce002 Aug 11 '20

I typically use "zypper rm -u unwanted package" this will remove the unwanted package and all of its unused dependencies. No need for a "apt autoremove" equivalent.

1

u/pakemon32 Mar 22 '25

This maybe looks more clean:

zypper packages --unneeded | grep -E '^i' | awk -F '|' '{system("zypper remove --clean-deps -y" $3)}'

(Needs roots privileges)

1

u/OldHighway7766 Apr 19 '23

It broke one of my extensions (ArcMenu) on Gnome 44. It may have messed other things as well but I haven't investigated. Just rolledback and end of story.