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 #> [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')] 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) if (-not $result.Success) { if ($result.StdErr -match '(?i)no such container') { Write-ALbuildLog -Level Warning "Container '$Name' does not exist; nothing to remove." return } 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'." } } |