{"record":{"id":"f4a8090c71899306","repo":"dotnet/wpf","slug":"printsystemexception-printqueue-purge","errorCode":null,"errorMessage":"PrintSystemException.PrintQueue.Purge","messagePattern":"PrintSystemException\\.PrintQueue\\.Purge","errorType":"exception","errorClass":"PrintQueueException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Printing/CPP/src/PrintQueue.cpp","lineNumber":1314,"sourceCode":"\n    Return Value\n        None\n--*/\nvoid\nPrintQueue::\nPurge(\n    void\n    )\n{\n    VerifyAccess();\n\n    try\n    {\n        printerThunkHandler->ThunkSetPrinter(PRINTER_CONTROL_PURGE);\n    }\n    catch (InternalPrintSystemException^ internalException)\n    {\n        throw CreatePrintQueueException(internalException->HResult,\n                                        \"PrintSystemException.PrintQueue.Purge\");\n    }\n}\n\n\nBoolean\nPrintQueue::PrintingIsCancelled::\nget(\n    void\n   )\n{\n    VerifyAccess();\n\n    return printingIsCancelled;\n}\n\nvoid\nPrintQueue::PrintingIsCancelled::","sourceCodeStart":1296,"sourceCodeEnd":1332,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Printing/CPP/src/PrintQueue.cpp#L1296-L1332","documentation":"PrintQueue.Purge converts a Win32 SetPrinter(PRINTER_CONTROL_PURGE) failure into a PrintSystemException tagged 'PrintSystemException.PrintQueue.Purge'. Purging deletes all queued print jobs; the HResult carries the native error code.","triggerScenarios":"Calling queue.Purge() when the spooler refuses the purge — access denied, jobs owned by other users that require admin rights, invalid printer name, or spooler outage.","commonSituations":"Non-administrator purging a shared print server queue; purging a queue whose driver/spooler is hung; stale PrintQueue handle after the printer was removed.","solutions":["Grant the user Manage Documents/Manage Printer rights on the queue before purging.","Purge per-job with PrintSystemJobInfo.Cancel() for jobs the user owns if full-purge rights are unavailable.","Confirm the queue exists and the spooler service is healthy before calling Purge().","Log the exception's HResult to distinguish access denied from spooler failures."],"exampleFix":"// before\nqueue.Purge();\n// after\ntry { queue.Purge(); }\ncatch (PrintSystemException ex) when (ex.HResult == unchecked((int)0x80070005))\n{\n    // fallback: cancel only jobs the current user owns\n    foreach (PrintSystemJobInfo job in queue.GetPrintJobInfoCollection())\n        if (job.Submitter == Environment.UserDomainName + \"\\\\\" + Environment.UserName) job.Cancel();\n}","handlingStrategy":"try-catch","validationCode":"bool canPurge = (queue.QueueStatus & PrintQueueStatus.Error) == 0;\nif (!canPurge) throw new InvalidOperationException(\"Queue in error state; purge would fail\");","typeGuard":null,"tryCatchPattern":"try { queue.Purge(); }\ncatch (PrintSystemException ex)\n{\n    if (ex.HResult == unchecked((int)0x80070005)) throw new UnauthorizedAccessException(\"Manage Documents permission required.\", ex);\n    throw;\n}","preventionTips":["Grant Manage Documents rights to accounts that purge shared queues.","Fall back to per-job Cancel() for user-owned jobs when purge is denied.","Confirm the queue exists on the server before purging.","Log the Win32 HResult alongside the queue name for diagnostics."],"tags":["printing","wpf","permissions"],"backgroundTag":"permission-denied","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}