When you install a program like postgresql, it installs several programs for its last version.

Once installed how remove all those packages? because using

apt-get remove postgresql

removes only that head package

  • Why do you think postgresql is a metapackage which actually it's not!?
    – Anwar
    Aug 23 '16 at 17:52

As apt-get autoremove (suggested by Aaron) will remove all "helper-packages" nothing seems to depend on any longer, sometimes you want to keep some of them for one reason or another. So if that concerns you, another possibility would be:

$(apt-cache depends postgresql|awk '{print "sudo apt-get remove "$NF}')

Using Bash as your shell, this would basically do the following:

  1. apt-cache depends postgresql would list all packages the postgresql depends on, including postgresql itself. But each line would look like depends on: <package> -- so we pipe the output to...
  2. awk '{print "sudo apt-get remove "$NF}' which would take the last word on each line (which is the package name), and prints it out after preceding it with our intended command: sudo apt-get remove (you could of course also use apt-get purge instead).
  3. finally, using the $() construct, we advise Bash to interpret the output as command to be executed.

You could alternatively replace the 3rd step and instead redirect the output into a file:

apt-cache depends postgresql|awk '{print "sudo apt-get remove "$NF}' >pg_remove.sh

And then inspect the file, optionally do some adjustments (such as commenting out/removing lines where you want to keep a package), and finally execute the script using

bash pg_remove.sh

Now you have a lot of possibilities to chose from :)

EDIT: Checking with more complex metapackages as e.g. lubuntu-desktop, above statements needs to be refined:

apt-cache depends <packageName>|grep "Depends on"|awk '{print "sudo apt-get remove "$NF}'

The grep is needed to restrict the result to dependencies (and skip the recommends etc.).

IMPORTANT: You should use this only for metapackages!!! otherwise, you may end up with an empty disk (e.g. postgresql-9.1 depends on libc6, and removing libc6 will certainly backfire as it is needed by a lot of packages).

So be careful, and better redirect to a file first (as explained) and investigate before execution.

  • apt-cache depends postgresql only shows that it depends on postgresql-9.1
    – Marc
    Jul 22 '12 at 19:46
  • That's correct -- but I wouldn't iterate with the remove; postgresql-9.1 itself depends e.g. on libc6, and if you remove that you probably get an empty disk (almost). This said, I must add you should only use this on meta-packages, for the given reasons. In your example, the meta-package seems to hold only this one package -- looking on others I see it gets much more complex, and my above answer would need to be refined (check e.g. with the lxde metapackage, or lubuntu-desktop...
    – Izzy
    Jul 22 '12 at 20:14
  • Amazing! This is so cool! This may be a silly question, but what do you do about purging the configuration files of the dependencies? If you used the command where you generate a shell script first that can be edited, do you see any problem with changing apt-get remove to apt-get purge or even` apt-get purge --auto-remove`?
    – Hee Jin
    Apr 26 '18 at 20:34
  • @Emily if you want that, just add the --purge parameter to the command, i.e. … awk '{print "sudo apt-get remove --purge "$NF}'.
    – Izzy
    Apr 27 '18 at 6:02

HIt Alt+Ctrl+T and run:

sudo apt-get autoremove

This command removes packages that are no longer needed, which is due to removing a package (such as postgresql) that depends on them.

You can always purge the configuration files with:

sudo apt-get autoremove --purge

To clean even further, use the following:

sudo apt-get install deborphan sudo apt-get autoremove --purge
  • The bad side is that it can not purge packages (remove configuration files) of that metapackage.
    – Marc
    Jul 22 '12 at 20:31

Simple follow the following commands :

sudo apt-get install deborphan
sudo apt-get autoremove --purge postgresql
sudo apt-get autoremove --purge `deborphan`

Not the answer you're looking for? Browse other questions tagged or ask your own question.