The SetJob function returns code 87 when it I pass the last parameter as JOB_CONTROL_DELETE. Basically, it works well, but it returns error on some jobs. These jobs exist in a queue and they are in a status Printed, so I think, that the OS doesn't
hold them. Eventually, I can remove jobs after several attempts and I don't know, what this count depends on. The worst case, when the job remains in a queue for days.
This problem seems to be specific for the Windows Server 2008 R2 as it doesn't occur on other operating systems, neither older, nor newer.