One Way to Automatically Remove Unneeded Packages

I have used this on a hobby Tumbleweed machine. Probably not a good idea to run it on a mission critical or production machine. If you’re worried about collateral damage, make a backup first. (Source: https://www.youtube.com/watch?v=wKXNr8tVcg0&t=40s.)

I had to run the command as root. Running it as sudo created an error.

zypper packages --unneeded |grep ^i|cut -d ‘|’ -f 3|xargs zypper remove --clean-deps

Also, if there are no unneeded packages, you’ll get an error message.

The error probably is because you need sudo for each instance of running zypper. Just putting it at the front won’t work, because zypper remove also needs to run as sudo. sudo doesn’t get used on the entire series of commands, just the first one.

You can accomplish something similar (not exactly the same) when removing packages by using the -u switch, (ie, zypper rm -u packagename) to remove dependencies that are no longer needed by other packages.

Additionally there seems to be a syntax error.
The difference is ‘|’ (not working from TO’s example) vs '|' (working).

Not working example from the TO:

testbox:~ # zypper packages --unneeded |grep ^i|cut -d ‘|’ -f 3|xargs zypper remove --clean-deps
’: command not found
cut: you must specify a list of bytes, characters, or fields
Try 'cut --help' for more information.
System management is locked by the application with pid 2458 (zypper).
Close this application before trying again.
testbox:~ # 

Working syntax:

testbox:~ # zypper packages --unneeded | grep ^i|cut -d '|' -f3|xargs zypper rm --clean-deps
Reading installed packages...
Resolving package dependencies...

The following 17 packages are going to be REMOVED:
  evolution-data-server-lang gtk-vnc-lang libKF5MediaWiki5 libavahi-gobject0 libavahi-ui-gtk3-0 libcacard0 libdrm_radeon1 libgtk-vnc-2_0-0 libgvnc-1_0-0 libphodav-3_0-0 libspice-client-glib-2_0-8
  libspice-client-glib-helper libspice-client-gtk-3_0-5 libusbredirhost1 libusbredirparser1 libwebrtc-audio-processing-2-1 vinagre-lang

17 packages to remove.

Package install size change:
              |         0 B    required by packages that will be installed
   -15.4 MiB  |  -   15.4 MiB  released by packages that will be removed

Backend:  classic_rpmtrans
Continue? [y/n/v/...? shows all options] (y): n

But yeah, the more clean and comfortable way is to remove unneeded packages when uninstalling a package as described by @hendersj

1 Like

The issue that zypper is missing a zypper rm --unneeded option, is open for a long time:

I’ll try running it as sudo next time. I copied and pasted exactly the command I’m using, which works as root. Not sure what happened with the " ’ " but maybe it got mangled somehow in the copying and pasting.

In the video, the guy ran it as sudo.

When you want to be of any service to your readers, then please use the “code tags” when posting code. Only then is there a chance that copy/paste of the above will work as intended. Now your ' characters are replaced by ‘ and ’ characters, which is not the same.

In the video, the guy run it in the root shell. How exactly the root shell was obtained is irrelevant. “Run as sudo” usually means - “run a single command using sudo”. Not “start root shell and run arbitrary commands”.

Fair enough. The command works for me and I’m glad the guy published the video. Also, if there are no unneeded packages, an error message will be returned.

That will delete plasma-session:

LANG=C zypper packages --unneeded | grep ^i|cut -d '|' -f3|xargs zypper rm --clean-deps
Installierte Pakete werden gelesen...
Paketabhängigkeiten werden aufgelöst...

Die folgenden 690 Pakete werden GELÖSCHT:
  acpica adjtimex aha akonadi-calendar-tools akonadi-calendar-tools-lang akonadi-import-wizard akonadi-import-wizard-lang akonadi-mime akonadi-plugin-calendar akonadi-plugin-contacts akonadi-plugin-mime
  akonadi-search akonadi-search-lang akregator akregator-lang alee-fonts aopalliance apache-commons-cli apache-commons-collections apache-commons-configuration2 apache-commons-exec AppStream AppStream-lang attr
  autofs baekmuk-bitmap-fonts baekmuk-ttf-fonts baloo5-file baloo5-file-lang baloo5-imports
.
.
.
.
.

`
plasma5-addons plasma5-addons-lang plasma5-desktop plasma5-desktop-emojier
  plasma5-desktop-lang plasma5-disks plasma5-disks-lang plasma5-session plasma5-systemmonitor plasma5-systemmonitor-lang plasma5-thunderbolt plasma5-thunderbolt-lang plexus-cipher plexus-classworlds
  plexus-compiler plexus-containers-component-annotations plexus-i18n plexus-interpolation plexus-languages plexus-sec-dispatcher polkit-kde-agent-5 polkit-kde-agent-5-lang 
.
.
.

And I use plasma5…

So please read it carefully.

I’m running KDE Plasma 6.3.2. No issues on my machine. But, as I said above, users should make a backup if consequential damage is a concern. And using this command on a mission critical or production machine is probably ill-advised.

Does zypper pa --unneeded output plasma5 packages?

Sure, but the ‘zypper rm’ isn’t part of the ‘sudo’ command because of the pipe characters.

zypper packages --unneeded |grep ^i|cut -d '|' -f 3|xargs zypper remove --clean-deps

xargs launches a new command with the output of the previous inputs as the rest of its command-line.

You can see the results without running them by changing it to:

zypper packages --unneeded |grep ^i|cut -d '|' -f 3|xargs echo zypper remove --clean-deps

The results you get with that should be something like (with the corrected single quotes):

$ zypper packages --unneeded | grep ^i | cut -d '|' -f 3 | xargs echo zypper remove --clean-deps
zypper remove --clean-deps cpp13 cuda-drivers gds-tools-11-8 gds-tools-12-0 gds-tools-12-2 gds-tools-12-3 gds-tools-12-4 gds-tools-12-5 ghc-cryptonite ghc-SHA gnome-shell-classic-xsession jupyter-jupyterlab-pygments jupyter-jupyterlab-widgets jupyter-server-terminals libavfilter9 libavutil58-32bit libBasicUsageEnvironment2 libdeflate0-32bit libftoa0_3 libIex-3_2-31-32bit libliveMedia112 libLLVM18 libLLVM18-32bit libmbedcrypto16 libmozjs-115-0 libOpenCOLLADABaseUtils0_3 libQt6WebChannelQuick6 libzopfli1 lua54-doc maim nsight-compute-2022.3.0 nsight-compute-2022.4.1 nsight-compute-2023.2.2 nsight-compute-2023.3.1 nsight-compute-2024.1.1 nsight-compute-2024.2.1 nsight-systems-2022.4.2 nsight-systems-2023.2.3 nsight-systems-2023.3.3 nsight-systems-2023.4.4 nsight-systems-2024.2.3 python311-alabaster python311-APScheduler python311-asn1crypto python311-async-lru python311-autocommand python311-azure-core python311-azure-mgmt-nspkg python311-beautifulsoup4 python311-bidict python311-bleach python311-cbor2 python311-comm python311-debugpy python311-defusedxml python311-entrypoints python311-fastjsonschema python311-Flask python311-gmpy2 python311-google-auth python311-grpcio-status python311-gssapi python311-httplib2 python311-imagesize python311-inflect python311-invoke python311-ipython python311-ipython_genutils python311-isodate python311-json5 python311-jupyter-events python311-jupyter-server-terminals python311-jupyterlab-pygments python311-jupyterlab-widgets python311-Mako python311-mistune python311-msal python311-natsort python311-nest-asyncio python311-overrides python311-pandocfilters python311-Parsley python311-portalocker python311-prometheus-client python311-proto-plus python311-pyproject-hooks python311-pysendfile python311-python-engineio python311-pyzmq python311-QDarkStyle python311-requests-oauthlib python311-scrypt python311-Send2Trash python311-snowballstemmer python311-sphinxcontrib-applehelp python311-sphinxcontrib-devhelp python311-sphinxcontrib-htmlhelp python311-sphinxcontrib-qthelp python311-sphinxcontrib-serializinghtml python311-sphinxcontrib-websupport python311-tinycss2 python311-tqdm python311-websocket-client python311-WTForms-lang python312-typeguard python313-typeguard qt6-gui-private-devel qt6-kmssupport-devel-static qt6-kmssupport-private-devel qt6-languageserver-devel-static qt6-opengl-private-devel qt6-platformsupport-devel-static qt6-quickcontrols2impl-devel qt6-quickdialogs2utils-private-devel v4l-utils w3m-inline-image

Everything after the echo is executed as a separate command. If it’s not sudo zypper remove --clean-deps [...], then the zypper remove command can’t run unless you execute the entire comand as root.

Starting the entire command-line with sudo doesn’t change that, because xarggs executes a new command in a non-root (non-sudo) context. It only would work if you said xargs sudo zypper remove [...].

I use commands like this a lot in managing my system. xargs is a very useful tool, and I usually do a test run using echo as the first test to make sure the constructed command is what I intended.

1 Like

Thanks. I thought it would be something like that but this kinda stuff is above my pay grade.

The script simply automates the following two commands:

zypper packages --unneeded

zypper remove [unneeded packages] --clean-deps

The script is no worse than carrying out the commands manually, which can be extremely laborious if there are several unneeded packages.