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.
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
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.
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.
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.
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.