How do I remove hundreds of automatically added network printers?

Using this command: lpstat -a we can see the installed printers and identify the name of the desired printer to keep, we can use the grep command also to filter the results like so: lpstat -a | grep <probable_name_of_printer>.

Then this little script can help:

  1. Run this command to check that the desired printer is not listed:

    lpstat -a | cut -d" " -f1 | sed -E '/<NAME_OF_PRINTER>/d' | grep <NAME_OF_PRINTER>
    
    • This should return nothing as it does the following:

      • lpstat -a: list installed printers
      • cut -d" " -f1: return only the names of the printers
      • sed -E '/<NAME_OF_PRINTER>/d': remove the name of the printer to keep from the output of the previous commands
      • grep <NAME_OF_PRINTER>: make sure the desired printer is not on the list
  2. If the above checks out; then run this command to remove every other printer that you don't need:

    sudo bash -c 'for i in $(lpstat -a | cut -d" " -f1 | sed -E '/<NAME_OF_PRINTER>/d'); do lpadmin -x "$i"; done'