Ignore packages that are not currently installed when using "apt-get remove"

15

4

I have a scenario where I'd like to remove a set of packages that may or may not be installed, and I'd like apt-get to remove those that are and silently ignore those that aren't. Something like:

apt-get remove foo bar baz

which, if foo and bar were installed but baz was not, would remove foo and bar without complaining about baz. Is there a way to do this?

Things I've tried that haven't worked, with cups-dbg as my scapegoat actually-installed package to be removed:

jcp@a-boyd:~$ sudo apt-get remove -y cups-dbg bogus-package
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package bogus-package

jcp@a-boyd:~$ sudo apt-get remove --ignore-missing cups-dbg bogus-package
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package bogus-package

jcp@a-boyd:~$ sudo apt-get remove --fix-broken cups-dbg bogus-package
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package bogus-package

I know I could do this with a shell script and some dpkg --list magic, but I'd like to avoid any complexity that's not absolutely necessary.

javawizard

Posted 2012-12-11T20:03:11.770

Reputation: 252

This answer worked for me in the end. Dumb fix for a dumb problem.

Note that there is actually a defect on Launchpad about this so feel free to contribute.

– Jeff – 2019-01-29T14:19:32.377

Answers

8

Is falling back to lower-level tool such as dpkg an option?

dpkg --remove foo bar libperl-dev
dpkg: warning: ignoring request to remove foo which isn't installed
dpkg: warning: ignoring request to remove bar which isn't installed
(Reading database ... 169132 files and directories currently installed.)
Removing libperl-dev ...

To remove packages config files use purge as below

dpkg --purge foo bar libperl-dev

barti_ddu

Posted 2012-12-11T20:03:11.770

Reputation: 1 286

1I should have mentioned that the packages to be removed can have reverse dependencies, and I'd like those to be removed too. Apt-get is therefore much better than dpkg, but I'll accept your answer since it seems that there isn't really a better way to do this. – javawizard – 2012-12-14T19:22:55.490

7

I use apt-get remove --purge (aka apt-get purge) for the dependency following with a list of packages. To handle packages that don't exist I filter out packages that are not installed with the following script.

pkgToRemoveListFull="cups-dbg bogus-package"
pkgToRemoveList=""
for pkgToRemove in $(echo $pkgToRemoveListFull); do
  $(dpkg --status $pkgToRemove &> /dev/null)
  if [[ $? -eq 0 ]]; then
    pkgToRemoveList="$pkgToRemoveList $pkgToRemove"
  fi
done
apt-get --yes --purge remove $pkgToRemoveList

Lucas

Posted 2012-12-11T20:03:11.770

Reputation: 171

3

For Debian ≤ 9, it is possible to just use aptitude instead of apt-get:

sudo aptitude remove -y cups-dbg bogus-package

Aptitude prints warnings, but continues to remove your packages nevertheless:

Couldn't find any package whose name or description matched "bogus-package"
...
Removing cups-dbg ...
...

If you want to purge (delete package config files) rather than remove (keep config files), note that aptitude only purges the directly given packages, while the unused dependencies are only removed. However, you can purge all removed packages in a second step:

apt-get -y purge $(dpkg -l | grep ^rc | awk '{print $2}')

vog

Posted 2012-12-11T20:03:11.770

Reputation: 187

ok. sometimes, the bogus-package just had the wrong name. so, in case we had like 500 packages, it would be better to analyze the bogus packages too and do some tricks (for example, numbers aftre the lackage name, etc.. so, can u please give some hints on how to do this.. – nyxee – 2017-08-15T07:40:23.087

@nyxee I propose to ask this as a new question, pointing out exactly what you want to achieve. – vog – 2017-08-16T09:17:20.130

1This would be a nice workaround except 1) aptitude is no longer installed by default on Ubuntu 18.04 and 2) aptitude has a pretty different (one could say weird) way to handle wildcards. – Jeff – 2019-01-29T14:17:51.387

This is not working for me on Debian 10. I see, Couldn't find any package whose name or description matched 'QUX' Unable to apply some actions, aborting – pdoherty926 – 2019-08-30T15:57:51.723

@pdoherty926 Thanks for the hint. I verified this and adjusted my answer accordingly. – vog – 2019-09-01T06:19:47.807

0

Another little 2-liner if anyone needs using apt:

purge_packages () {
  matchedPackages="$(echo "$(apt list --installed $* 2>/dev/null)" | grep -v '^Listing\.\.\.' | sed -s 's|[/ ].*||' | tr '\n' ' ' | sed 's/ *$//;s/^ *//')"
  [[ -n "$matchedPackages" ]] && apt purge -yq $matchedPackages
}

Explination:

apt list --installed $*         # Lists packages matched from function args, the --installed flag limits results to installed packages

2>/dev/null                     # Disregard the warning about using this command in a script

grep -v '^Listing\.\.\.'        # Remove the first line of output that says "Listing..." or "Listing... Done"

sed -s 's|[/ ].*||'             # Remove the rest of the line after the package name (I'm checking for / or space though it looks like just the slash is needed but package names will never have a space)

tr '\n' ' '                     # Put it all on one line separated by spaces

sed 's/ *$//;s/^ *//'           # Remove trailing and leading spaces from the line so it will be blank during the test next line if nothing was matched

[[ -n "$matchedPackages" ]]     # Check if any packages were matched

apt purge -yq $matchedPackages  # Purge them!

Meir

Posted 2012-12-11T20:03:11.770

Reputation: 1

Welcome to SuperUser! Would you add more description to your answer as to what your script does, and how it does it? – El8dN8 – 2019-09-05T00:47:22.707

@El8dN8 Added explination – Meir – 2019-09-05T15:24:12.747