dotnet/wpf · error · PrintQueueException

PrintSystemException.PrintQueue.Purge

Error message

PrintSystemException.PrintQueue.Purge

What it means

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.

Solutions

  1. Grant the user Manage Documents/Manage Printer rights on the queue before purging.
  2. Purge per-job with PrintSystemJobInfo.Cancel() for jobs the user owns if full-purge rights are unavailable.
  3. Confirm the queue exists and the spooler service is healthy before calling Purge().
  4. Log the exception's HResult to distinguish access denied from spooler failures.

Example fix

// before
queue.Purge();
// after
try { queue.Purge(); }
catch (PrintSystemException ex) when (ex.HResult == unchecked((int)0x80070005))
{
    // fallback: cancel only jobs the current user owns
    foreach (PrintSystemJobInfo job in queue.GetPrintJobInfoCollection())
        if (job.Submitter == Environment.UserDomainName + "\\" + Environment.UserName) job.Cancel();
}
Defensive patterns

Strategy: try-catch

Validate before calling

bool canPurge = (queue.QueueStatus & PrintQueueStatus.Error) == 0;
if (!canPurge) throw new InvalidOperationException("Queue in error state; purge would fail");

Try / catch

try { queue.Purge(); }
catch (PrintSystemException ex)
{
    if (ex.HResult == unchecked((int)0x80070005)) throw new UnauthorizedAccessException("Manage Documents permission required.", ex);
    throw;
}

Prevention

When it happens

Trigger: 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.

Common situations: Non-administrator purging a shared print server queue; purging a queue whose driver/spooler is hung; stale PrintQueue handle after the printer was removed.

Understand the failure class

Background: Permission denied / not authorized / 403 Forbidden: access-control rejections when the caller lacks the required role, grant, or ownership — this error's family across 18 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/f4a8090c71899306. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Printing/CPP/src/PrintQueue.cpp:1314

    Return Value
        None
--*/
void
PrintQueue::
Purge(
    void
    )
{
    VerifyAccess();

    try
    {
        printerThunkHandler->ThunkSetPrinter(PRINTER_CONTROL_PURGE);
    }
    catch (InternalPrintSystemException^ internalException)
    {
        throw CreatePrintQueueException(internalException->HResult,
                                        "PrintSystemException.PrintQueue.Purge");
    }
}


Boolean
PrintQueue::PrintingIsCancelled::
get(
    void
   )
{
    VerifyAccess();

    return printingIsCancelled;
}

void
PrintQueue::PrintingIsCancelled::

View on GitHub (pinned to 81131a70a4)