"Unable to locate package" error during apt-get uninstall

I'm trying to clean out old CUDA installations by running

sudo apt-get purge nvidia-*

but I get the errors

E: Unable to locate package nvidia-diag-driver-local-repo-ubuntu1604-390.46_1.0-1_amd64.deb
E: Couldn't find any package by glob 'nvidia-diag-driver-local-repo-ubuntu1604-390.46_1.0-1_amd64.deb'
E: Couldn't find any package by regex 'nvidia-diag-driver-local-repo-ubuntu1604-390.46_1.0-1_amd64.deb'

I had deleted the deb file previously after finishing the install. I tried re-downloading the .deb file and running dpkg -i on it, but that doesn't fix the error either.

dpkg -l | grep nvidia gives

ii  nvidia-390                                      390.48-0ubuntu0~gpu16.04.3                   amd64        NVIDIA binary driver - version 390.48
ii  nvidia-390-dev                                  390.48-0ubuntu0~gpu16.04.3                   amd64        NVIDIA binary Xorg driver development files
rc  nvidia-diag-driver-local-repo-ubuntu1604-390.46 1.0-1                                        amd64        nvidia-diag-driver-local repository configuration files
ii  nvidia-modprobe                                 390.46-0ubuntu1                              amd64        Load the NVIDIA kernel driver and create device files
ii  nvidia-opencl-icd-390                           390.48-0ubuntu0~gpu16.04.3                   amd64        NVIDIA OpenCL ICD
ii  nvidia-prime                                    0.8.2                                        amd64        Tools to enable NVIDIA's Prime
ii  nvidia-settings                                 396.18-0ubuntu0~gpu16.04.1                   amd64        Tool for configuring the NVIDIA graphics driver

Solution 1:

You are globbing current files in the directory. Instead of what you are running, try:

sudo apt-get purge nvidia-\*
# or...
sudo apt-get purge 'nvidia-*'

The main clue that tells me this is the case: You tried to remove packages named like blah.deb in the apt-get purge output. Of course packages aren't normally named that way...

To see what I mean, check ls -l in your current directory. You should see the .deb files you are matching. If you wouldn't have those files in your current directory, the 'nvidia-*' would pass through unaltered to the apt-get purge command.

Good rule to remember: It's always best to protect your arguments with single quotes ' to prevent unintended shell globbing and other expansions.