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?
To the point
Needles to say that:
Purging hundreds of installed packages at once is a bad idea regardless of the reason for that.
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:or using a shell loop like:
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:Around the point
Demonstration:
Notice the three steps before committing the
purge
action:These are self explanatory and although the package
golang
is not installed:But, APT could locate it in its cached packages list and thus could take a decision regarding it:
and:
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 can't take the decision and thus exits after printing an error:
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 doingpurge
/remove
or eveninstall
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:first.
The
apt purge
command will fail if any of the packages you specify do not exist. This is becauseapt purge
removes all of the files associated with a package, including configuration files. If a package does not exist, then there are no files to remove, so theapt purge
command fails.To force the purge of existing installed packages, even if one of the packages does not exist, you can use the
--force-remove
option. For example, the following command will purge the packagespack1
,pack2
, andpack3
, even ifpack3
does not exist:The
--force-remove
option tellsapt purge
to ignore any errors that occur when it tries to remove a package that does not exist. This means that theapt purge
command will still succeed, even if one of the packages you specify does not exist.Here is a breakdown of the command:
sudo
- This tells the command to be run as root.apt purge
- This is the command to purge a package.--force-remove
- This option tellsapt purge
to ignore any errors that occur when it tries to remove a package that does not exist.pack1
pack2
pack3
- These are the packages that you want to purge.If you're facing the issue where the entire process fails due to a missing package while attempting to apt purge or remove multiple packages at once, there is a way to ensure the removal of the existing installed packages without being hindered by the absence of a single package.
To accomplish this, you can make use of a bash command called "apt-get --ignore-missing". This command allows you to continue with the removal process even if some packages are not found.
Here's an example of how you can modify your GitHub action step to include this command:
By using the "--ignore-missing" flag, apt-get will disregard any packages that it cannot find and proceed with purging the remaining packages. This ensures that pack1 and pack2, if present, will still be purged even if pack3 is not found.
This way, you can confidently purge the existing installed packages without the process failing due to the absence of a single package.
Hope this solution helps you achieve the desired outcome in your GitHub action!