zypper equivalent for apt autoremove

Thursday, February 24, 2022

I recently started using openSUSE for no reason other than that it was different from what I’d been using, and I wanted a change. One question that quickly arose was how to do the equivalent of Debian/Ubuntu’s apt autoremove, and from the 30 seconds I spent searching, a lot of other people had that question too. None of them had answers, though, or if they did they were incomplete.

Noting the caveats here and here the following worked for me:

# zypper rm -u $(zypper -q -s 10 pa --unneeded | grep ^i | cut -d : -f 3 | uniq)

For some reason, there’s no option that means “only print the package names,” but—because this is apparently something we need—there are eleven different “table styles” for the output. In any case, one of the table styles (-s 10) prints out the table with a colon separating each column, so at that point it just becomes standard Unix text processing.

Note that if you’ve got a colon in a repository or package name, this won’t work properly. And obviously, but maybe most importantly, you should only run this interactively, and check to see whether you’re about to remove something that you want to keep.