How does apt-get purge work? Why it couldn't remove broken package in one pass?

I’ve tried installing droidcam to solve another question following instruction here: Install DroidCam (Wireless Android Webcam) Client In Ubuntu Via PPA

The installation fails with post-script error, full log found here.

Now, I want to clean it up and remove it completely. So, I run sudo apt-get purge droidcam. But I was surprised that I have to run it 3 times to fully clean its installation.

$ sudo apt-get purge droidcam        
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  linux-headers-3.19.0-15 linux-headers-3.19.0-15-generic linux-image-3.19.0-15-generic linux-image-extra-3.19.0-15-generic
Use 'apt-get autoremove' to remove them.
The following packages will be REMOVED:
  droidcam*
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
1 not fully installed or removed.
After this operation, 1,529 kB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 239094 files and directories currently installed.)
Removing droidcam (1.3-0ubuntu0) ...
rmmod: ERROR: Module v4l2loopback_dc is not currently loaded
dpkg: error processing package droidcam (--purge):
 subprocess installed post-removal script returned error exit status 1
Processing triggers for gnome-menus (3.10.1-0ubuntu5) ...
Processing triggers for bamfdaemon (0.5.1+15.04.20150202-0ubuntu1) ...
Rebuilding /usr/share/applications/bamf-2.index...
Processing triggers for desktop-file-utils (0.22-1ubuntu3) ...
Processing triggers for mime-support (3.58ubuntu1) ...
Processing triggers for hicolor-icon-theme (0.14-0ubuntu1) ...
Errors were encountered while processing:
 droidcam
E: Sub-process /usr/bin/dpkg returned an error code (1)

$ sudo apt-get purge droidcam 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  linux-headers-3.19.0-15 linux-headers-3.19.0-15-generic linux-image-3.19.0-15-generic linux-image-extra-3.19.0-15-generic
Use 'apt-get autoremove' to remove them.
The following packages will be REMOVED:
  droidcam
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
1 not fully installed or removed.
After this operation, 1,529 kB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 239088 files and directories currently installed.)
Removing droidcam (1.3-0ubuntu0) ...

$ sudo apt-get purge droidcam 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  linux-headers-3.19.0-15 linux-headers-3.19.0-15-generic linux-image-3.19.0-15-generic linux-image-extra-3.19.0-15-generic
Use 'apt-get autoremove' to remove them.
The following packages will be REMOVED:
  droidcam*
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] y
(Reading database ... 239087 files and directories currently installed.)
Removing droidcam (1.3-0ubuntu0) ...
Purging configuration files for droidcam (1.3-0ubuntu0) ...
dpkg: warning: while removing droidcam, directory '/opt' not empty so not removed

$ sudo apt-get purge droidcam 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Package 'droidcam' is not installed, so not removed
The following packages were automatically installed and are no longer required:
  linux-headers-3.19.0-15 linux-headers-3.19.0-15-generic linux-image-3.19.0-15-generic linux-image-extra-3.19.0-15-generic
Use 'apt-get autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

What I want to know:

  • Why, I need to run it multiple times?
  • How does apt-get purge work? I though apt-get purge will remove all installed files with one pass.

My first expectation, may be droidcam is not well packaged.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

This and many other packages have problems to be removed after a problem during the installation. My solution, if a script fails, look into the folder /var/lib/dpkg/info/. There are four types of scripts

  1. package_name.preinst – pre-installation script
  2. package_name.postinst – post-installation script
  3. package_name.prerm – pre-removal script
  4. package_name.postrm – post-removal script

Look into the scripts: It is usually very easy to find the problem immediately. Often it is just a service that can not be started or stopped. Or as in your case, an unloaded module. If you want to only remove the package, remove the offending code and start deleting again.

At very problematic cases Sometimes the only way to delete the entire script down to the line exit 0, currently e.g. nginx 🙁

And as @kos says, remove the line set -e. But I’ve never tested.

And start a petition for more robust scripts =)


Some examples

How can I remove a dpkg broken by the Brother MFC-8840 deb driver?

AVG Error (reinstall ubuntu)

Failing to install doc-base

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply