zypper Cookbook: Autoremove Packages and Remove Orphaned Packages

As I learned how to autoremove unnecessary packages as you remove a package and also how to identify orphaned packages, I marveled at the beauty of zypper and the excellent work of all its contributors. Not only does zypper have the functionality to do just about anything related to repos and packages, it has sensible online help right at your fingertips.

Before I show how to perform these two tasks this post is primarily about, let me show you two easy ways to figure out whether zypper can do what you want to do.

The first thing is global help on what is available with zypper.

zypper help

	zypper [--global-options]  [--command-options] [arguments]

  Global Options:
	--help, -h		Help.
	--version, -V		Output the version number.
	--promptids		Output a list of zypper's user prompts.
	--config, -c 	Use specified config file instead of the default.
	--userdata 	User defined transaction id used in history and plugins.
	--quiet, -q		Suppress normal output, print only error
	--verbose, -v		Increase verbosity.
	--no-abbrev, -A		Do not abbreviate text in tables.
	--table-style, -s	Table style (integer).
	--rug-compatible, -r	Turn on rug compatibility.
	--non-interactive, -n	Do not ask anything, use default answers
				Do not treat patches as interactive, which have
				the rebootSuggested-flag set.
	--xmlout, -x		Switch to XML output.
	--ignore-unknown, -i	Ignore unknown packages.

	--reposd-dir, -D 	Use alternative repository definition file
	--cache-dir, -C 	Use alternative directory for all caches.
	--raw-cache-dir 	Use alternative raw meta-data cache directory.
	--solv-cache-dir 	Use alternative solv file cache directory.
	--pkg-cache-dir 	Use alternative package cache directory.

     Repository Options:
	--no-gpg-checks		Ignore GPG check failures and continue.
	--gpg-auto-import-keys	Automatically trust and import new repository
				signing keys.
	--plus-repo, -p 	Use an additional repository.
	--disable-repositories	Do not read meta-data from repositories.
	--no-refresh		Do not refresh the repositories.
	--no-cd			Ignore CD/DVD repositories.
	--no-remote		Ignore remote repositories.

     Target Options:
	--root, -R 	Operate on a different root directory.
				Do not read installed packages.

	help, ?			Print help.
	shell, sh		Accept multiple commands at once.

     Repository Management:
	repos, lr		List all defined repositories.
	addrepo, ar		Add a new repository.
	removerepo, rr		Remove specified repository.
	renamerepo, nr		Rename specified repository.
	modifyrepo, mr		Modify specified repository.
	refresh, ref		Refresh all repositories.
	clean			Clean local caches.

     Service Management:
	services, ls		List all defined services.
	addservice, as		Add a new service.
	modifyservice, ms	Modify specified service.
	removeservice, rs	Remove specified service.
	refresh-services, refs	Refresh all services.

     Software Management:
	install, in		Install packages.
	remove, rm		Remove packages.
	verify, ve		Verify integrity of package dependencies.
	source-install, si	Install source packages and their build
	install-new-recommends, inr
				Install newly added packages recommended
				by installed packages.

     Update Management:
	update, up		Update installed packages with newer versions.
	list-updates, lu	List available updates.
	patch			Install needed patches.
	list-patches, lp	List needed patches.
	dist-upgrade, dup	Perform a distribution upgrade.
	patch-check, pchk	Check for patches.

	search, se		Search for packages matching a pattern.
	info, if		Show full information for specified packages.
	patch-info		Show full information for specified patches.
	pattern-info		Show full information for specified patterns.
	product-info		Show full information for specified products.
	patches, pch		List all available patches.
	packages, pa		List all available packages.
	patterns, pt		List all available patterns.
	products, pd		List all available products.
	what-provides, wp	List packages providing specified capability.

     Package Locks:
	addlock, al		Add a package lock.
	removelock, rl		Remove a package lock.
	locks, ll		List current package locks.
	cleanlocks, cl		Remove unused locks.

     Other Commands:
	versioncmp, vcmp	Compare two version strings.
	targetos, tos		Print the target operating system ID string.
	licenses		Print report about licenses and EULAs of
				installed packages.
	download		Download rpms specified on the commandline to a local directory.
	source-download		Download source rpms for all installed packages
				to a local directory.

Type 'zypper help ' to get command-specific help.

The second thing is the last line in the output of zypper help: how to get help on the commands available with zypper. For example, let’s lookup help for packages command.

zypper help packages

packages (pa) [options] [repository] ...

List all packages available in specified repositories.

  Command options:

-r, --repo   Just another means to specify repository.
-i, --installed-only      Show only installed packages.
-u, --uninstalled-only    Show only packages which are not installed.
    --orphaned            Show packages which are orphaned (without repository).
    --suggested           Show packages which are suggested.
    --recommended         Show packages which are recommended.
    --unneeded            Show packages which are unneeded.
-N, --sort-by-name        Sort the list by package name.
-R, --sort-by-repo        Sort the list by repository.

Lookie here now, we found a way to identify orphaned packages.

zypper packages --orphaned

Loading repository data...
Reading installed packages...
S | Repository | Name                          | Version   | Arch  
i | @System    | openSUSE-release-livecd-gnome | 13.1-1.10 | x86_64

Go right ahead and remove the orphaned packages that offend you so.

We can go one step further and make sure any packages that only depend on the package you’re removing are also removed.

zypper help remove

remove (rm) [options]  ...

Remove packages with specified capabilities.
A capability is NAME[.ARCH][OP], where OP is one
of <, =, >.

  Command options:
-r, --repo     Load only the specified repository.
-t, --type            Type of package (package, patch, pattern, product).
                            Default: package.
-n, --name                  Select packages by plain name, not by capability.
-C, --capability            Select packages by capability.
    --debug-solver          Create solver test case for debugging.
-R, --no-force-resolution   Do not force the solver to find solution,
                            let it ask.
    --force-resolution      Force the solver to find a solution (even
                            an aggressive one).
-u, --clean-deps            Automatically remove unneeded dependencies.
-U, --no-clean-deps         No automatic removal of unneeded dependencies.
-D, --dry-run               Test the removal, do not actually remove.

We can remove, for example nodejs, and all its dependencies (that are not dependencies for any other package) this way:

sudo zypper remove --clean-deps nodejs

I hope by now you are as convinced as I am that zypper is not only functional but (gasp!) beautiful as well.

2 Responses to zypper Cookbook: Autoremove Packages and Remove Orphaned Packages

  1. I don’t know something equal

    sudo zypper remove –clean-deps nodejs

    in apt. This command in zypper is so wonderful. I need it so much in apt. Thank you for this comprehensive article.

  2. On debian systems you can always run apt-get autoremove to remove all uneeded packages from your system.
    apt-get even suggests you to do that when you remove something and it finds that there are dependencies not needed any more on your system.

%d bloggers like this: