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

15,389

Solution 1

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

Solution 2

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

Solution 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}')
Share:
15,389

Related videos on Youtube

javawizard
Author by

javawizard

Updated on September 18, 2022

Comments

  • javawizard
    javawizard over 1 year

    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
    javawizard over 11 years
    I 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.
  • nyxee
    nyxee over 6 years
    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..
  • vog
    vog over 6 years
    @nyxee I propose to ask this as a new question, pointing out exactly what you want to achieve.
  • Jeff
    Jeff over 5 years
    This 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.
  • pdoherty926
    pdoherty926 over 4 years
    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
  • vog
    vog over 4 years
    @pdoherty926 Thanks for the hint. I verified this and adjusted my answer accordingly.
  • El8dN8
    El8dN8 over 4 years
    Welcome to SuperUser! Would you add more description to your answer as to what your script does, and how it does it?
  • Meir
    Meir over 4 years
    @El8dN8 Added explination
  • Admin
    Admin about 2 years
    apt makes it easy but doesn't have a stable CLI. The input and output could change breaking this code. Can you update it to use a different command? Such as dpkg-query --list $* and grepping for ^iito find installed packages.