20

I've recently been reading through a lot of questions/answers/opinions about removing packages and dependencies. There's a lot of different ways to do it, some being overly convoluted and some being insufficient/incomplete. I want to ask here in one place what others think is the best ways to do go about clearing out a package and everything including configs, dependencies, and the dependencies' configs. I'll offer my method as well that came from a lot of trial and error and hair-pulling.

4 Answers 4

24

Warning: This can remove packages that you might still want. If this happens just reinstall them. But honestly when you're installing/uninstalling something that's gonna make big changes to your system...why not go ahead and backup first.

The most thorough, method I've ever come up with to completely remove a package PLUS its dependencies PLUS all configs including those configs of dependencies and do a little housecleaning is this where PACKAGENAME is the main package to be removed:

  • Log out from the desktop and press Ctrl+Alt+F1 then login to TTY1 and run the following commands:

    sudo apt-get purge <PACKAGENAME>
    sudo apt-get purge $(apt-cache depends <PACKAGENAME> | awk '{ print $2 }' | tr '\n' ' ')
    sudo apt-get autoremove
    sudo apt-get update
    sudo apt-get check
    sudo apt-get -f install
    sudo apt-get autoclean
    
  • Restart if needed

    sudo shutdown -r now
    

So what's going on in the second line? The pipes take the output from apt-cache depends and reformat it. The first pipe awk '{ print $2 }' takes the output of apt-cache depends and prints, or "echoes", only the second column. Without it you'd also have in the list another column which is the dependency type, i.e. "depends", "recommends", etc. Then the second pipe tr '\n' ' ' takes that result and removes, or truncates (hence tr), the newline(s) and replaces them with a space that separates the names. All of this returns a "space-delimited" list of the names of all dependency packages of PACKAGENAME that's format-friendly for use with multiple package input to purge command-option.

This works especially well for meta packages. I run the last command "sudo apt-get -f install" at the end to check for possible broken packages and fix them after making so many changes all at once. I especially do this anytime I add a "DE" and want to go back. Just recently I used this after installing GNOME and it even fixed the fact that only purging Gnome with autoremove after still left my GRUB changed and left the login option in the DM. When I had tried out lubuntu-desktop it fixed some conflicts and removed the entries from the DM login that were left by purge and autoremove.

A similar method which works well, is probably safer, but still doesn't always get everything is:

sudo apt-get --purge autoremove PACKAGENAME

Which may or may not need to be followed up with:

sudo apt-get update
sudo apt-get check
sudo apt-get -f install
sudo apt-get autoclean
4
  • It looks like a great way to do it, I understand all but the last part of the 2nd line "awk '{ print $2 }' | tr '\n' ' ' )" could you please elaborate on what those 2 pipes are doing?
    – TrailRider
    Dec 12, 2015 at 7:04
  • Sure. I edited to add some explanation. Dec 12, 2015 at 10:44
  • 3
    So ... you're purging the depends without checking whether other packages might also depend on them? Dec 12, 2015 at 11:57
  • Yes, there is a possibility of adverse side effects which I noted. I find myself using this method from time to time as a last ditch effort when remove PACKAGENAME, autoremove PACKAGENAME, --purge autoremove PACKAGENAME, etc, still doesn't do the job. This happens usually with meta packages. I just pay attention to what's listed to be removed before pressing "Y" and if something comes up that doesn't look right I cancel and investigate. An addition of an extra pipe, before awk, with grep recommends, grep replaces, or grep conflicts, etc, can be used also if needed to step through slower. Dec 13, 2015 at 12:30
6

I also use aptitude purge for that:

sudo aptitude purge <PACKAGENAME>

This command

  • removes the dependencies
  • without removing the dependencies which are also dependencies of some other installed package,
  • but i'm not sure if it removes the dependencies' configs.
1
  • 1
    I can confirm it does not remove confgs. Oct 18, 2018 at 5:33
3

autoremove is probably what you want.

For example, with remove, only one package is to be removed:

$ sudo apt remove nvidia-cuda-toolkit
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  g++-8 javascript-common libaccinj64-10.1 libcublas10 libcublaslt10 libcudart10.1 libcufft10 libcufftw10 libcuinj64-10.1 libcupti-dev libcupti-doc libcupti10.1 libcurand10 libcusolver10 libcusolvermg10
  libcusparse10 libegl-dev libgl-dev libgl1-mesa-dev libgles-dev libgles1 libglvnd-dev libglx-dev libjs-jquery libjs-underscore libncurses5 libnppc10 libnppial10 libnppicc10 libnppicom10 libnppidei10
  libnppif10 libnppig10 libnppim10 libnppist10 libnppisu10 libnppitc10 libnpps10 libnvblas10 libnvgraph10 libnvidia-common-510 libnvidia-ml-dev libnvjpeg10 libnvrtc10.1 libnvtoolsext1 libnvvm3 libopengl-dev
  libpthread-stubs0-dev libstdc++-8-dev libthrust-dev libvdpau-dev libx11-dev libxau-dev libxcb1-dev libxdmcp-dev node-html5shiv nsight-compute nsight-systems nvidia-cuda-dev nvidia-cuda-doc nvidia-cuda-gdb
  nvidia-opencl-dev nvidia-profiler nvidia-visual-profiler ocl-icd-libopencl1 ocl-icd-opencl-dev opencl-c-headers openjdk-8-jre x11proto-core-dev x11proto-dev xorg-sgml-doctools xtrans-dev
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
  nvidia-cuda-toolkit
0 upgraded, 0 newly installed, 1 to remove and 95 not upgraded.
After this operation, 67.1 MB disk space will be freed.

with autoremote:

$ sudo apt autoremove nvidia-cuda-toolkit
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  g++-8 javascript-common libaccinj64-10.1 libcublas10 libcublaslt10 libcudart10.1 libcufft10 libcufftw10 libcuinj64-10.1 libcupti-dev libcupti-doc libcupti10.1 libcurand10 libcusolver10 libcusolvermg10
  libcusparse10 libegl-dev libgl-dev libgl1-mesa-dev libgles-dev libgles1 libglvnd-dev libglx-dev libjs-jquery libjs-underscore libncurses5 libnppc10 libnppial10 libnppicc10 libnppicom10 libnppidei10
  libnppif10 libnppig10 libnppim10 libnppist10 libnppisu10 libnppitc10 libnpps10 libnvblas10 libnvgraph10 libnvidia-common-510 libnvidia-ml-dev libnvjpeg10 libnvrtc10.1 libnvtoolsext1 libnvvm3 libopengl-dev
  libpthread-stubs0-dev libstdc++-8-dev libthrust-dev libvdpau-dev libx11-dev libxau-dev libxcb1-dev libxdmcp-dev node-html5shiv nsight-compute nsight-systems nvidia-cuda-dev nvidia-cuda-doc nvidia-cuda-gdb
  nvidia-cuda-toolkit nvidia-opencl-dev nvidia-profiler nvidia-visual-profiler ocl-icd-libopencl1 ocl-icd-opencl-dev opencl-c-headers openjdk-8-jre x11proto-core-dev x11proto-dev xorg-sgml-doctools xtrans-dev
0 upgraded, 0 newly installed, 73 to remove and 95 not upgraded.
After this operation, 3,654 MB disk space will be freed.
1
  • Further, since OP is asking about how to do deep cleanup, we want to use the purge command instead of just remove and the --purge command line option along the autoremove. Then everything should be gone. Of course the package maintainer has to make sure of that... Nov 3, 2022 at 1:05
0

This will remove specified package(s), all auto-installed packages and all their configs:

sudo apt-get -s purge <PKG> | grep '^ ' | tr -d '*' | xargs -o sudo apt-get purge

Or as root:

apt-get purge `apt-get -s purge <PKG> | grep '^ ' | tr -d '*'`

Substitute <PKG> with package or packages you want to remove completely.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .