Linux+: Applications 13 – Package Removal with dpkg


Jarret W. Buse

Linux+: Applications 13 – Package Removal with dpkg

Since most users tend to install a lot of packages, or programs, to their Linux system, some packages will need to be removed. Sometimes packages are no longer needed because they are outdated, do not work properly, or even not useful anymore. Some users may even require obtaining more of the disk space back to install other packages. No matter the reason, package removal is usually a necessity.

The command dpkg has the following syntax:

dpkg <actions> <options> <package_names>

NOTE: The command must have one action and zero or more options. Most of these require Root privileges, so place sudo before dpkg.

The actions covered in this article are as listed:

· -r (--remove)
· -P (--purge)
· -a
· --pending
· --get-selections
· --clear-selections
· --set-selections

NOTE: The last three actions can fall under other categories as well, but I place them here to be used more for package removal. The three “selections” actions can be used for install, hold, deinstall and purge operations.

The process which occurs during a package removal is as follows:

· Before any files can be removed, the “prerm” script must be executed. The script causes any daemons associated with the package to be stopped. If the daemon continues to run, then specific files cannot be deleted.
· All files associated with the package are removed
· The “postrm” script is run which is associated with the package being removed. The script causes all file associations for the package to be removed as well as any links created by the package installation. All files created by the package installation are removed.

NOTE: The script files can be found in /var/lib/dpkg/info. The scripts are found in the file package_name.script. For instance, the “postrm” script for the synaptic package would be the file at /var/lib/dpkg/info/synaptic.postrm.

Now, to cover the various actions given above and give examples for each. For the examples, I will use the Debian package for the AVG anti-virus program which is called “avg2013flx-r3118-a6926.i386.deb”.

NOTE: To easily download a package, use the command “sudo apt-get download <package_name>”. The package_name can be replaced with a specified package. Enter part of the package name and press TAB (twice if needed when multiple files exist for the partial name you entered). The file should be downloaded to the current folder and it may be best to be in the HOME directory.

Be aware that the command dpkg does not download any files, but only manages local files. For package removal, this is not an issue.

The subcommands, or actions, are as follows:

· -r (--remove) – used to remove a package, but conffiles are left in the case that the package is reinstalled and configuration settings are left from previous install

NOTE: During a package removal, errors may occur which will not allow a package to be removed. One of the main reasons is that dependencies are not correct on the system. To fix dependency issues, perform the following commands in a terminal:

1. sudo apt-get update --fix-missing
2. sudo dpkg --configure -a
3. sudo apt-get install -f

Once these steps have been completed, then retry the package removal again.

If the error is that a package (first package) depends on the package you are removing (second package), then you will need to remove the first package before the second package.

For example, to remove the AVG package once it is installed, but leave the configuration files in case AVG is installed again with the settings remaining, the following command should be used:

sudo dpkg –r avg2013flx

· -P (--purge) – used to remove a package, but conffiles are also deleted (unlike with the –r action)

The command to remove the AVG package as well as the configuration files, use the following command:

sudo dpkg -P avg2013flx

· -a (--pending) – used instead of a package name causes all packages which are unpacked and marked to be removed or purged will be removed or purged as the case may be

Package status is listed in the file /var/lib/dpkg/status. Here, each package is listed with a status of install, hold, deinstall or purge. All of these are straightforward except for hold. The status “hold” is used to place a package in a state that will not be affected by the dpkg command.

NOTE: The status is changed by the apt-get command, but can also be changed by the dpkg command using the set-selections action.

To start, let’s look at the --get-selections action. To see the current status of all of all installed packages, use the command:

· dpkg --get-selections

A list of all packages within the file /var/lib/dpkg/status will be displayed as well as the status. Of course, the list can be redirected to a file using the “>” symbol and a filename.

The “--clear-selections” action is used to change the status of all non-essential packages to “deinstall”.

Once packages are marked as “deinstall” and you wish to carry out the removal of the packages, the command is:

sudo apt-get dselect-upgrade

Now, all packages with a status of “deinstall” will be removed. This will also work for the “purge” status.

You can now get the status of packages, but you need to set the status of packages. To do this, make a text file of package names. After each package, press TAB and then set the status to “deinstall” or “purge” (remember if you set a package to “hold” it causes dpkg to bypass it). Specify changes in the text file and save it. To make these specifications to take effect, you need to set the selections as follows:

sudo dpkg --set-selections <file_name>

For example, if the text file is called “packages.txt” and is located in the HOME folder, the command would be:

sudo dpkg --set-selections ~/packages.txt

All the settings in the text file are now in the status file. And the apt-get command can be executed to make the changes. Now, a dilemma may occur at this point. For example, let’s say we set a package called “zip” to purge and then delete it with the apt-get command. If you run the get-selections action, you will see that “file-roller” is now set to the status as “deinstall”. Why? The file-roller package depends on the zip package. Its dependencies have been broken so it needs to be removed. Be careful when manually managing the status file. Many problems can occur because of the dependency issues.

Be aware that any packages which has not been installed and set to the status of “install” will be installed.


  • slide.jpg
    3.1 KB · Views: 37,276

$100 Digital Ocean Credit
Get a free VM to test out Linux! Hosting Donations
Consider making a donation

Members online