Deleting Printer

jamfmdm
New Contributor

Is there a way that we can delete printer that user added? we have a open directory that teacher can pick what printer they want to add. now we have new system that only uses one virtual printer, and we already push that one using LPD. what we want to happen is to remove all printer that they add and only printer one printer will stay the one that we push.

1 ACCEPTED SOLUTION

franton
Valued Contributor III

Removing all printers from a Mac before adding new ones can be done with this script.

#!/bin/bash

lpstat -p | awk '{print $2}' | while read printer
do
echo "Deleting Printer:" $printer
lpadmin -x $printer
done

View solution in original post

28 REPLIES 28

franton
Valued Contributor III

Removing all printers from a Mac before adding new ones can be done with this script.

#!/bin/bash

lpstat -p | awk '{print $2}' | while read printer
do
echo "Deleting Printer:" $printer
lpadmin -x $printer
done

This script has broken the printer setting pane. 

Now I can't see any new printer after adding it.
I tried to right-click and reset printer settings or to reset the system preferences completely and nothing helped.
Please help me 

jamfmdm
New Contributor

@franton how about deleting specific printer like not all the printer only the one that they added using our open directory?

franton
Valued Contributor III

@jamfmdm Well as long as you know the name of it, that's a simple "lpadmin -x printername" on it's own. That assumes the printer isn't in Casper already, otherwise you could use a policy scoped to that machine to remove it.

jamfmdm
New Contributor

@franton

Tried this and it work it deleted all the printer

!/bin/bash

lpstat -p | awk '{print $2}' | while read printer
do
echo "Deleting Printer:" $printer
lpadmin -x $printer
done

When i tried this it did not delete the printer that i want

!/bin/bash

lpstat -p | awk '{print $2}' | while read printer
do
echo "Deleting Printer:" UASD-PRINTER-SF-3111-Xerox-4112
lpadmin -x UASD-PRINTER-SF-3111-Xerox-4112
done

franton
Valued Contributor III

@jamfmdm Ok what happens if you just run this on a terminal window?

sudo lpadmin -x UASD-PRINTER-SF-3111-Xerox-4112

jamfmdm
New Contributor

@franton This is what happen.

bda50c2abbd04ac49953874f8b34f183

franton
Valued Contributor III

Ok interesting. What's the output from

sudo cat /etc/cups/printers.conf

jamfmdm
New Contributor

@franton i got it. it should be GF_Printer_SF_3111_Xerox_4112 for the printer name

franton
Valued Contributor III

Excellent. I had a feeling it was something like that.

jamfmdm
New Contributor

@franton hello i have one more question. i made the script something like this

lpadmin -x UASD_PRINTER_GF_Copy_Center_Xerox4112
lpadmin -x UASD_PRINTER_SF_3111_Xerox_4112
lpadmin -x UASD_PRINTER_FF_2108_XeroxPro275
lpadmin -x UASD_PRINTER_SF_312A_HPLJ4014
lpadmin -x UASD_PRINTER_MPH_M12_Xerox5845
lpadmin -x UASD_PRINTER_GF_Copy_Center_SharpColor_MX_5110n

this is the printer we want to remove to all the teacher, but some printers are not installed to teacher laptop and some are installed, but for some reason when i push the policy most of the macbook is failed and this is the error message that is showing.

9e04709b88954b2fa575c74f3e87ef4c
c6417923867f430e9b1a6434d2f0fa16

franton
Valued Contributor III

Yes that will happen if you attempt to remove a printer that doesn't exist on the system. You will have to script around this.

jamfmdm
New Contributor

@franton so there is wrong with my script right that why it's not working? but for some are working not sure why.

jamfmdm
New Contributor

@franton so there is wrong with my script right that why it's not working? but for some are working not sure why.

franton
Valued Contributor III

It depends entirely on how those printers were originally created on those macs. You've already found that the display name of the printer doesn't always match the CUPS name with the _ instead of - . Hardcoding the printer names will produce uneven results because if they were manually created, then human error during creation will produce different results.

That's why I recommended removing all printers before adding your replacement printer queue.

franton
Valued Contributor III

If you go back to a Mac where your script failed on, and run:

lpstat -p | awk '{print $2}'

That will give you a list of all the CUPS printer names on the Mac. You can then compare to your script. I'm almost certain you will find differences in the names.

Philein
New Contributor III

Hi, the script is giving me this error:

 

Script result: Deleting Printer: Xerox_C60
Deleting Printer: Xerox_C60 lpadmin: The printer or class does not exist.
Deleting Printer: Xerox_C60 lpadmin: The printer or class does not exist.
Deleting Printer: Xerox_C60 lpadmin: The printer or class does not exist.
Deleting Printer: Xerox_C60 lpadmin: The printer or class does not exist.

 

Anyway the name is correct... any tips?

mallej
New Contributor III

hello, i don´t know if this wasn't before, but

lpstat -p | awk '{print $2}'

will output the Queue Name in quotes and that results in not finding that printer when using

#!/bin/bash

lpstat -p | awk '{print $2}' | while read printer
do
echo "Deleting Printer:" $printer
lpadmin -x $printer
done

with Sierra.

Anyone having the same problem?
Anyone with enough awk skills to remove the quotes?

michaelhusar
Contributor II

@jensm We had some „ and “ and used successfully:

lpstat -p | cut -d' ' -f2 | tr -d „ | tr -d “ | xargs -I{} lpadmin -x {}

stevewood
Honored Contributor II
Honored Contributor II

You can also use wildcards to remove the printers:

printers=($(lpstat -p | awk '{print $2}' | sed '/^$/d'))

for i in "${printers[@]}"
do

    if [[ ${i} == *"2831 C9"* ]]; then
        lpadmin -x ${i}
    elif [[ ${i} == *"2831_C9"* ]]; then
        lpadmin -x ${i}
    fi

done

saulv
New Contributor III

Franton... Thank you. Your input on this subject helped me tremendously!

palmna
Contributor

@stevewood, I like what you posted but I'm guessing (because I'm not great at scripting) that your snippet is only looking for 1 instance of the printer name containing "2831 c9". How could I adapt this to loop through and delete all printers with "2831 c9" in their name?

stevewood
Honored Contributor II
Honored Contributor II

@palmna

Actually, the wildcards before and after each of those should match any printer with that in the name. So, this line:

if [[ ${i} == "2831 C9" ]]; then

Is saying "if the printer name contains 2831 C9, then you may proceed". And this line:

elif [[ ${i} == "2831_C9" ]]; then

is saying "otherwise (else) if the printer name contains 2831_C9, then you may proceed".

The asterisk before and after those names means match anything that contains the value in quotes. If you had an asterisk only at the front it would match names that end with the pattern, and if the asterisk was only at the end it would match names that start with the pattern.

Make sense?

palmna
Contributor

@stevewood Makes sense. Thanks for the explaination.

franton
Valued Contributor III

Follow up to my original answer. If I just want to nuke all printers, i'm now running this:

/System/Library/Frameworks/ApplicationServices.framework/Frameworks/PrintCore.framework/Versions/A/printtool -f --reset

scottb
Honored Contributor

@franton - is this the same as Control-Clicking within the Printers section of "Printers & Scanners" and selecting "Reset printing system..."?

 

scottb_0-1629928211024.png

 

franton
Valued Contributor III

Precisely that!

scottb
Honored Contributor

I can't tell you how long I've looked for a way to do that!  Cheers, sir!