functions/public/Remove-KlippyJobQueue.ps1

function Remove-KlippyJobQueue {
    <#
    .SYNOPSIS
        Removes jobs from the print queue.

    .DESCRIPTION
        Removes specific jobs from the print queue or clears the entire queue.

    .PARAMETER Id
        The unique identifier of the printer.

    .PARAMETER PrinterName
        The friendly name of the printer.

    .PARAMETER InputObject
        A printer object or queued job from pipeline input.

    .PARAMETER JobId
        The specific job ID(s) to remove from the queue.

    .PARAMETER All
        Clear all jobs from the queue.

    .PARAMETER Force
        Skip confirmation prompt.

    .EXAMPLE
        Remove-KlippyJobQueue -JobId "0000001"
        Removes a specific job from the queue.

    .EXAMPLE
        Remove-KlippyJobQueue -All -Force
        Clears all jobs from the queue without confirmation.

    .EXAMPLE
        Get-KlippyJobQueue | Where-Object { $_.PSTypeName -eq 'KlippyCLI.QueuedJob' } | Remove-KlippyJobQueue
        Removes all queued jobs via pipeline.

    .OUTPUTS
        None.
    #>

    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High', DefaultParameterSetName = 'ByJobId')]
    param(
        [Parameter(ParameterSetName = 'ById')]
        [Parameter(ParameterSetName = 'ByIdAll')]
        [ValidateNotNullOrEmpty()]
        [string]$Id,

        [Parameter(ParameterSetName = 'ByName')]
        [Parameter(ParameterSetName = 'ByNameAll')]
        [ValidateNotNullOrEmpty()]
        [string]$PrinterName,

        [Parameter(ParameterSetName = 'ByObject', ValueFromPipeline = $true)]
        [Parameter(ParameterSetName = 'ByObjectAll', ValueFromPipeline = $true)]
        [PSCustomObject]$InputObject,

        [Parameter(Mandatory = $true, ParameterSetName = 'ById')]
        [Parameter(Mandatory = $true, ParameterSetName = 'ByName')]
        [Parameter(ParameterSetName = 'ByObject', ValueFromPipelineByPropertyName = $true)]
        [string[]]$JobId,

        [Parameter(Mandatory = $true, ParameterSetName = 'ByIdAll')]
        [Parameter(Mandatory = $true, ParameterSetName = 'ByNameAll')]
        [Parameter(Mandatory = $true, ParameterSetName = 'ByObjectAll')]
        [switch]$All,

        [Parameter()]
        [switch]$Force
    )

    begin {
        $jobsToRemove = [System.Collections.Generic.List[string]]::new()
        $printer = $null
    }

    process {
        # Resolve printer
        $resolveParams = @{}
        if ($Id) { $resolveParams['Id'] = $Id }
        elseif ($PrinterName) { $resolveParams['PrinterName'] = $PrinterName }
        elseif ($InputObject) {
            if ($InputObject.PSTypeName -eq 'KlippyCLI.QueuedJob') {
                $resolveParams['Id'] = $InputObject.PrinterId
                if ($InputObject.JobId -and -not $All) {
                    $jobsToRemove.Add($InputObject.JobId)
                }
            }
            else {
                $resolveParams['InputObject'] = $InputObject
            }
        }

        if (-not $printer) {
            $printer = Resolve-KlippyPrinterTarget @resolveParams
        }

        # Collect job IDs from parameter
        if ($JobId) {
            foreach ($jid in $JobId) {
                $jobsToRemove.Add($jid)
            }
        }
    }

    end {
        if (-not $printer) {
            Write-Error "No printer specified."
            return
        }

        try {
            if ($All) {
                $action = "Clear ALL jobs from queue"
                if ($Force -or $PSCmdlet.ShouldProcess($printer.PrinterName, $action)) {
                    $null = Invoke-KlippyJsonRpc -Printer $printer -Method "server/job_queue/clear"
                    Write-Host "Job queue cleared on '$($printer.PrinterName)'." -ForegroundColor Green
                }
            }
            elseif ($jobsToRemove.Count -gt 0) {
                $action = "Remove $($jobsToRemove.Count) job(s) from queue"
                if ($Force -or $PSCmdlet.ShouldProcess($printer.PrinterName, $action)) {
                    # Build the job_ids parameter
                    $jobIdsJson = $jobsToRemove | ConvertTo-Json -Compress
                    if ($jobsToRemove.Count -eq 1) {
                        $jobIdsJson = "[$jobIdsJson]"
                    }
                    $encodedJobIds = [System.Uri]::EscapeDataString($jobIdsJson)

                    $endpoint = "server/job_queue/delete?job_ids=$encodedJobIds"
                    $null = Invoke-KlippyJsonRpc -Printer $printer -Method $endpoint

                    Write-Host "Removed $($jobsToRemove.Count) job(s) from queue on '$($printer.PrinterName)'." -ForegroundColor Green
                    foreach ($jid in $jobsToRemove) {
                        Write-Verbose " Removed: $jid"
                    }
                }
            }
            else {
                Write-Warning "No jobs specified to remove. Use -JobId or -All."
            }
        }
        catch {
            Write-Error "Failed to remove from job queue on '$($printer.PrinterName)': $_"
        }
    }
}