Score:3

APT purge available packages even when some package is not available

us flag

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?

Raffa avatar
jp flag
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.
cc flag
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.
Raffa avatar
jp flag
@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'`
Score:4
jp flag

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.

us flag
Thanks a lot. I'm aware that `apt purge -y` might be dangerous, but this is really just for a [github action runner](https://github.com/jfoclpf/geoapi.pt/blob/main/.github/workflows/docker.yml), thus no harm done if anything goes wrong.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.