3

I have a github action with a step for freeing space and it has a apt purge command followed by hundreds of packages. But if one package is not available, the all process fails

If you apt purge or remove a huge list of several packages at once, how to force the purge of existing installed packages?

Example:

sudo apt purge pack1 pack2 pack3 -y

if pack1 and pack2 exist but pack3 does not exists, the process fails

E: Unable to locate package 'pack3'

and pack1 and pack2 are not purged.

How to force removal of pack1 and pack2 even if one of the remaining doesn't exist?

  • 1
    In this case I would use sudo dpkg --purge pack1 pack2 pack3 for a faster solution or if APT is a must, pass packages one at a time by e.g. piping package names to xargs -n 1 sudo apt purge -y or in a shell loop e.g. for p in "pack1" "pack2" "pack3"; do sudo apt purge -y "$p"; done and similar. – Raffa Jun 30 '23 at 13:59
  • On my Ubuntu 22.04 (and all previous versions), apt purge (without the -y) will list the missing, and ask for confirmation on the purge, a "y" will then purge. I typically use this on kernels, getting the list of possible packages from the version number, then issuing the purge on the output of that command -- so many are missing, but the command always works. – ubfan1 Jun 30 '23 at 16:10
  • @ubfan1 even with the -y that will work as long as all the packages can be located ,,, But neither will work when a package can not be located i.e. E: Unable to locate package 'pack3' – Raffa Jun 30 '23 at 16:35
  • 2
    With apologies on behalf of the community, it looks like your question was spammed by a trio of bots that are posting multiple AI-generated answers on questions. Unfortunately, the majority of the Moderators on Stack Exchange sites (including Ask Ubuntu) are on strike primarily because the parent company has reduced their ability to remove these bogus answers. We're hoping that things get back to normal shortly, but in the meantime, we're having to deal with stuff like this. – NotTheDr01ds Jul 05 '23 at 14:10
  • Thank you @NotTheDr01ds, I was not aware that AI was already spamming SO – João Pimentel Ferreira Jul 06 '23 at 15:11

1 Answers1

4

To the point

Needles to say that:

  1. Purging hundreds of installed packages at once is a bad idea regardless of the reason for that.

  2. Using APT's option -y to automatically answer yes, is even a worse idea.

You probably know that, but I had to note it ... On to your question:

AFAIK APT can't do that ... APT does more than the basic actions of just attempt to install, remove, purge ... etc. ... It resolves dependencies, avoids and warns about conflicts, substitutes some removed packages ... etc. ... Thus it has to parse/check all passed packages against its cached available packages database before it begins committing any user specified action on them.

Therefore, your best bet in that situation is to pass those packages one at a time to APT using xargs like:

echo "pack1 pack2 pack3" | xargs -n 1 apt purge

or using a shell loop like:

for p in "pack1" "pack2" "pack3"
  do
    apt purge "$p"
    done

or similar solutions.

On the other hand, if using APT is not a must for you, then dpkg should do the job without exiting on such packages "Unable to locate packages" and can be used like so:

dpkg --purge pack1 pack2 pack3

Around the point

Demonstration:

$ apt -s purge -y vlc golang
NOTE: This is only a simulation!
      apt needs root privileges for real execution.
      Keep also in mind that locking is deactivated,
      so don't depend on the relevance to the real current situation!
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package 'golang' is not installed, so not removed
The following packages will be REMOVED:
  vlc*
0 to upgrade, 0 to newly install, 1 to remove and 5 not to upgrade.
Purg vlc [3.0.16-1build7]

Notice the three steps before committing the purge action:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done

These are self explanatory and although the package golang is not installed:

Package 'golang' is not installed, ...

But, APT could locate it in its cached packages list and thus could take a decision regarding it:

... so not removed 

and:

The following packages will be REMOVED:
  vlc*
...

That is normal and APT will carry on removing the installed package/s

But in case of a package that APT can't locate in its cached packages lists:

$ apt -s purge -y vlc non-locatable-package
NOTE: This is only a simulation!
      apt needs root privileges for real execution.
      Keep also in mind that locking is deactivated,
      so don't depend on the relevance to the real current situation!
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package non-locatable-package

APT can't take the decision and thus exits after printing an error:

E: Unable to locate package non-locatable-package

Important notice:

As seen in the demonstration above, APT checks its cached packages lists obtained via the update action and therefore, an up to date packages list/s is vital to correct functionality of APT and doing purge/remove or even install actions with an outdated packages list/s might result in an unexpected result removing unintended packages ... See for example apt-get install unexpectedly removed Firefox ... Therefore, make sure the packages list/s are up to date by running:

sudo apt update

first.

Raffa
  • 32,237