Modules/businessdev.ALbuild.Containers/Public/Remove-BcContainer.ps1

function Remove-BcContainer {
    <#
    .SYNOPSIS
        Removes a Business Central container (and its anonymous volumes).
 
    .PARAMETER Name
        Container name.
 
    .PARAMETER DockerExecutable
        The Docker executable to use (default 'docker').
 
    .EXAMPLE
        Remove-BcContainer -Name bld
    #>

    # No ConfirmImpact='High': ALbuild runs primarily non-interactively (pipelines, the MCP, the VS Code
    # extension), where a High-impact confirmation prompt has no host UI and ShouldProcess throws a
    # NullReferenceException. -WhatIf still works and callers can opt into -Confirm; destructive intent is
    # gated by the consumers (e.g. the MCP's approval flow), not by an auto-prompt here.
    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
        [ValidateNotNullOrEmpty()]
        [Alias('ContainerName')]
        [string] $Name,

        [string] $DockerExecutable = 'docker'
    )

    process {
        if (-not $PSCmdlet.ShouldProcess($Name, 'Remove Business Central container')) { return }

        $result = Invoke-BcDocker -DockerExecutable $DockerExecutable -Quiet -PassThru `
            -Arguments @('rm', '--force', '--volumes', $Name)

        # 'docker rm --force' exits 0 even when the container does not exist - it just writes a
        # "No such container" message to stderr - so check the message, not only the exit code, before
        # reporting a successful removal. (A genuine removal writes nothing matching this to stderr.)
        if ($result.StdErr -match '(?i)no such container') {
            Write-ALbuildLog -Level Warning "Container '$Name' does not exist; nothing to remove."
            return
        }
        if (-not $result.Success) {
            throw "Failed to remove container '$Name': $($result.StdErr.Trim())"
        }

        # Clean up the host folder bind-mounted into the container (see Get-BcContainerHostShare).
        $hostShare = Get-BcContainerHostShare -Name $Name
        if (Test-Path -LiteralPath $hostShare) { Remove-Item -LiteralPath $hostShare -Recurse -Force -ErrorAction SilentlyContinue }

        # Remove any certificate ALbuild trusted for this container (best effort; no-op if none).
        try { [void](Unregister-BcContainerCertificate -Name $Name -Confirm:$false) }
        catch { Write-ALbuildLog -Level Warning "Could not remove trusted certificate for '$Name': $($_.Exception.Message)" }

        Write-ALbuildLog -Level Success "Removed container '$Name'."
    }
}