zypper "autoremove" feature?

Good day friends,
I’m partially new to Opensuse (been stuck on the debian train) and i was wondering if there is an “autoremove” feature like APT has in the Debians and *buntus to clean up my system?
I have exhausted my google searches as there is no clear answer and some very old articles,also i really don’t want to break my system:)
Any help would be appreciated!

zypper remove --clean-deps ...

Thanks so much!

@arvidjaar:

I too have been wondering about the zypper command that matches up with “autoremove” option . . . available with apt . . . that searches out unneeded packages and removes them . . . in a universal fashion . . . independently from other upgrade operations.

I ran your basic command and it responds with “too few arguments” . . . meaning it wants me to name the packages that I want removed, rather than in debian and ubuntu, mint, etc . . . “sudo apt autoremove” searches and finds the packages it suggests it will remove and if we type “y” . . . it is done.

I’ve run enough zypper’s that I have seen packages listed that are “to be removed” . . . along with the other zypper operations it will run . . . but, so far like @Truuks99 I haven’t found the similar command like “autoremove” offered by apt services . . . .

n_s

Not exactly –

  • apt(-get) autoremove

autoremove is used to remove packages that were automatically installed to satisfy dependencies for other packages and are now no longer needed as dependencies changed or the package(s) needing them were removed in the meantime.

You should check that the list does not include applications you have grown to like even though they were once installed just as a dependency of another package. You can mark such a package as manually installed by using apt-mark(8). Packages which you have installed explicitly via install are also never proposed for automatic removal.

On the other hand –

  • zypper remove --clean-deps «Package Name»

Automatically remove dependencies which become unneeded after removal of requested packages.

Automatically installed packagesPackages added by the dependency solver in order to resolve a user’s request are remembered as having been automatically installed. They may later be removed, if no more user installed packages depend on them (e.g. by zypper remove --clean-deps).

In the Status column the search command distinguishes between user installed packages (i+) and automatically installed packages (i).

Yes, the Zypper man page text is confusing – it’s missing the point that, dependencies are only automatically cleaned up if, when a package is removed, the “–clean-deps” option is used – when a package is removed …

Please examine ‘/etc/zypp/zypper.conf’ and ‘/etc/zypp/zypp.conf’ – there ain’t no man page entries – only the comments in the configuration files …

  • In ‘/etc/zypp/zypp.conf’ …

##
## EXPERTS ONLY: Cleanup when deleting packages. Whether the solver should
## per default try to remove packages exclusively required by the ones he's
## asked to delete.
##
## This option should be used on a case by case basis, enabled via
## command line options or switches the applications offer. Changing
## the global default on a system where unattended actions are performed,
## may easily damage your system.
##
## CHANGING THE DEFAULT IS NOT RECOMMENDED.
##
## Valid values:  boolean
## Default value: false
##
# solver.cleandepsOnRemove = false

And, there’s this:


##
## Whether dist upgrade should remove a products dropped packages.
##
## A new product may suggest a list of old and no longer supported
## packages (dropped packages). Performing a dist upgrade the solver
## may try to delete them, even if they do not cause any dependency
## problem.
##
## Turning this option off, the solver will not try to remove those
## packages unless they actually do cause dependency trouble. You may
## do the cleanup manually, or simply leave them installed as long
## as you don't need the disk space.
##
## Valid values:        Boolean
## Default value:       true
##
# solver.upgradeRemoveDroppedPackages = true

“zypper verify --details --force-resolution” doesn’t indicate any packages which are no longer needed – the packages indicated by the YaST Software Management as being “Not needed” are not pulled out by the Zypper command …

  • There’s a Yum plugin for cleaning up installed packages which seems to have been integrated into the current (non-Yum) YaST …
  • Sadly, it seems that, there’s nothing in either Zypper nor RPM which can walk through all the dependencies of all installed packages to determine which installed packages have no dependencies to any other installed package …
  • Yes, one can write some code to check the lists of packages “rpm --query --requires/–recommends/–suggests «RPM Package Name»” against the list of installed packages, to determine which packages are not required/recommended/suggested by any other installed package but, the YaST Software Management tab does that so, why bother?

I know. This question comes up every now and then and I patiently wait for someone to actually submit bug report requesting this enhancement.

Maybe this is what you’re looking for?

zypper packages --unneeded


Also, the following will remove any packages that have no active repo but depending on your setup this could remove things you don’t want.

zypper packages --orphaned


This only lists packages. I suggest running this after updating the system. In the interim of a “zypper ref” and a “zypper dup/up”, it may show outdated packages, so always update before running this command.

Here’s the command I run:

mapfile -t unneeded < <(zypper --quiet pa --unneeded | awk '$1 == "i" { print $5, "-", $7 }')
(( ${#unneeded@]} )) && zypper --quiet rm --clean-deps --details "${unneeded@]}" 

Eventually it will appear packages that we need. I think “zypper addlock” is the only way around.

In the interest of science I tried to run that command . . . didn’t exactly work???

sudo mapfile -t unneeded < <(zypper --quiet pa --unneeded | awk '$1 == "i" { print $5, "-", $7 }')
[sudo] password for root: 
Sorry, try again.
[sudo] password for root: 
sudo: mapfile: command not found
n1@n1:~/Desktop> mapfile -t unneeded < <(zypper --quiet pa --unneeded | awk '$1 == "i" { print $5, "-", $7 }')

Then the “computer” revised the command to what shows below, which I tried to run:

n1@n1:~/Desktop> (( ${#unneeded@]} )) && zypper --quiet rm --clean-deps --details "${unneeded@]}"
Root privileges are required to run this command.
n1@n1:~/Desktop> sudo (( ${#unneeded@]} )) && zypper --quiet rm --clean-deps --details "${unneeded@]}"
bash: syntax error near unexpected token `('

A FYI -
Here on openSUSE,
We do not have an aversion to “su” or “su -” instead of sudo…
And a number of commands will not work using sudo, they require full elevated permissions provided by “su” or "su -"ad

For most situations you should be able to sudo instead of su, but since everything is easier in a su environment (you don’t have to elevate each and every command like sudo), try it you’ll like it.

TSU

$ type mapfile
mapfile is a shell builtin
$ bash --version
GNU bash, version 5.1.4(1)-release (x86_64-suse-linux-gnu) 
$ mapfile --help
mapfile: mapfile -d delim] -n count] -O origin] -s count] -t] -u fd] -C callback] -c quantum] [array]
Read lines from the standard input into an indexed array variable.

Sorry for the mess. It means that bash on Leap don’t provide this command. You might want to find an alternative to mapfile. Also, sudo should be placed before “zypper rm”, not just in front of the whole line. Or login as root with “su -” and then running that commands.

@tsu2:

Thanks . . . I did think of that when it kicked back the “root privileges” comment . . . but there were other problems that seemed to be showing up such that I decided I had spent enough time messing with it; and the command still didn’t seem to match the simplicity and function of “sudp apt autoremove” . . . .

Since I’m in a multi-boot environment I don’t always think of “su” . . . which does work quite well in OpenSUSE . . . but, for some reason in my ubuntu based installs “su” doesn’t respond to admin password, it requires some other steps to do . . . but indeed, in OpenSUSE it’s operational from the install . . . .

OPs question remains and I support their inquiry . . . .

Yes, “zypper packages --unneeded” and “zypper packages --orphaned” only list the unneeded/orphaned packages – it doesn’t automatically do the housekeeping …

  • Which is an issue with the “apt(-get) autoremove” feature –

You should check that the list does not include applications you have grown to like even though they were once installed just as a dependency of another package. You can mark such a package as manually installed by using apt-mark(8). Packages which you have installed explicitly via install are also never proposed for automatic removal.

IOW, the apt “auto-magically remove” feature recommends that a manual check should be performed …
[HR][/HR]AFAICS, there is a Zypper/APT difference in the way that Packages automatically installed due to dependencies versus, Packages manually installed by the user, are handled –

  • With APT, the user has to manually mark the packages which have been explicitly installed.
  • With ZYpp – the system management library behind Zypper – the marking is done automatically – zypper man page …

Automatically installed packagesPackages added by the dependency solver in order to resolve a user’s request are remembered as having been automatically installed. They may later be removed, if no more user installed packages depend on them (e.g. by zypper remove --clean-deps).

In the Status column the search command distinguishes between user installed packages (i+) and automatically installed packages (i).

Really appreciated this post which seemed to be the way to go and helped me get rib of a stack of older rubbish.

I ran this after running zypper dup and having accepted the removals ran zypper dup again. A bit of a surprise as there were 271 updates to do. This is quite normal with TW which I am running so I let the update proceed.

I then thought I would try the above script again. No problem but on looking closer and not being too familiar with bash, is this two script one command or two. I ask because first time I ran it it ran as one but now when I copy and paste it to my terminal it shows as two separate commands.

That’s surprising indeed, unless there were a new snapshot in between "dup"s.

They’re two separate command lines. Here’s the command sequence again with “sudo” so you can run as a regular user. Copy/pasting into terminal should just work.

mapfile -t unneeded < <(zypper --quiet pa --unneeded | awk '$1 == "i" { print $5, "-", $7 }')
(( ${#unneeded@]} )) && sudo zypper --quiet rm --clean-deps --details "${unneeded@]}"
# cat cleandeps.sh
#!/bin/sh
mapfile -t unneeded < <(zypper --quiet pa --unneeded | awk '$1 == "i" { print $5, "-", $7 }') (( ${#unneeded@]} ))
sudo zypper --quiet rm --clean-deps --details "${unneeded@]}"
# ./cleandeps.sh
/usr/local/bin/cleandeps.sh: line 2: syntax error near unexpected token `&lt;'
/usr/local/bin/cleandeps.sh: line 2: `mapfile -t unneeded < <(zypper --quiet pa --unneeded | awk '$1 == "i" { print $5, "-", $7 }') (( ${#unneeded@]} ))'
# cat cleandeps.sh
#!/bin/sh
mapfile -t unneeded < <(zypper --quiet pa --unneeded | awk '$1 == "i" { print $5, "-", $7 }') (( ${#unneeded@]} )) && sudo zypper --quiet rm --clean-deps --details "${unneeded@]}"
# ./cleandeps.sh
/usr/local/bin/cleandeps.sh: line 2: syntax error near unexpected token `&lt;'
/usr/local/bin/cleandeps.sh: line 2: `mapfile -t unneeded < <(zypper --quiet pa --unneeded | awk '$1 == "i" { print $5, "-", $7 }') (( ${#unneeded@]} )) && sudo zypper --quiet rm --clean-deps --details "${unneeded@]}"'

:frowning:

I run all my scripts with /bin/bash. This one isn’t POSIX compliant.

This worked for me: sudo zypper rm $(zypper pa --unneeded | awk '/i / {print $3}' FS='|' | uniq | tr -d ' ')