20 Sept 2016

How to uninstall obsolete dependent packages

How much rubbish does your system have?

Do you say none? Then let me ask how old is your system? How often have you installed and removed software?

If your system is not fresh and you installed and removed a good dozen different applications using Synaptic or any other package manager, it is more likely than not that you have not removed all the obsolete dependent packages. They could be installed as a pre-requisite for the application you wanted, but they are not automatically uninstalled when you decide to uninstall the main application.
Do you remember being in this situation?

Let me show you how you can deal with this situation in distibutions using Synaptic package manager.

Say, I have installed an application that required some additional dependent packages to be installed.

Now I need to remove all of them to restore the system status quo.



First of all, start Synaptic package manager. It will ask you for the root password. Once started navigate to menu File – History. This will open up a new window where the history of your Synaptic installations is listed.

Click the section in the left part of the window with the date and time of your installation.
Synaptic history

The right part of the window now shows the list of packages that were installed in the particular session. Of course, you can copy-paste each individual package name and remove them one by one in Synaptic. It is easy if there are just a handful of packages like on this screenshot. But if there are dozens of additional packages you installed the task requires some automation.

Let's start with selecting and copying the list of packages into the LibreOffice Calc spreadsheet.

When importing the text in Calc, it may ask you about the Text Import options. If this is the case, you can specify opening bracket "(" as a separator. If this does not happen, select the inserted range in the spreadsheet and call up the menu item Data – Text to Columns. Specify opening bracket "(" as a separator and confirm.
Columns separator is the opening bracket

In either case the result will be a spreadsheet with two columns. The left column (say, column A) will have the package names and the right column (say, column B) will have some technical information about the packages
Converted text

You can now remove the contents of column B and copy the value of cell A1 to cell B1.

Then insert the following formula into column B2
=B1&" "&A2
Copy this formula to all the cells in column B below B2 along your filled cells in column A. LibreOffice Calc will automatically adjust cell numbers in the formula. As a result, your last cell will contain the full list of packages that you installed in the Synaptic session separated by spaces.

Now copy the contents of the last cell in column B, close Synaptic package manager and open Terminal window. Type in
sudo apt-get purge
and then paste the copied contents of your LibreOffice Calc cell.
Terminal with the list of packages

You can use remove option instead of purge in this command. This is your personal preference. You can check differences between purge and remove options in many Internet resources, for example here.

Once you enter your root password in the terminal window, the uninstall process starts. It may ask you some questions along the way. Please be careful with these questions as they may be very important.

For example, if you installed some other applications after the application you are removing now, the newer application could require the same dependent package. If you remove the shared package, your new application may be automatically uninstalled too. It is a highly recommended to start the bulk uninstall routine from the last "bad" session, and still check the questions carefully.

As a result of these simple steps you have now removed the unnecessary package and its unnecessary dependencies.

3 comments:

  1. Or you could go to a command line, su to root, and type:
    apt-get autoremove

    ReplyDelete
  2. The hints below concern Debian based systems (Ubuntu, Mint).
    To remove packages which were installed automatically and are no more needed :
    apt-get --purge autoremove
    To remove "orphaned" packages (packages which are not needed by some other ones) install deborphan :
    apt-get get install deborphan
    and then run it. If the output is not empty run :
    apt-get --purge remove ` deborphan `
    and repeat this step until it does nothing.
    Last but not least you may have still have config files of removed packages. To check run :
    dpkg -l | egrep ^rc
    If the output is not not empty you can remove the remains of those packages with
    dpkg --purge
    Automatizing this process is left as an exercise to the reader (hint : "awk '{ print $2 }'" prints the second word of each line of the input)
    Happy cleaning ...

    ReplyDelete
  3. above you should read :
    dpkg --purge package_name

    ReplyDelete