Modules/businessdev.ALbuild.Environments/Public/Remove-BcExpiredEnvironment.ps1

function Remove-BcExpiredEnvironment {
    <#
    .SYNOPSIS
        Removes all expired ALbuild Business Central environments (licensed).
 
    .DESCRIPTION
        Finds ALbuild environments whose expiry has passed and removes them (and their Traefik
        configuration). Intended to be run on a schedule.
 
    .PARAMETER TraefikConfigFolder
        Folder containing the environments' Traefik dynamic configs to delete.
 
    .PARAMETER DockerExecutable
        The Docker executable to use (default 'docker').
 
    .OUTPUTS
        The names of the removed environments.
    #>

    [CmdletBinding(SupportsShouldProcess)]
    [OutputType([string])]
    param(
        [string] $TraefikConfigFolder,
        [string] $DockerExecutable = 'docker'
    )

    Assert-ALbuildLicensed -Feature 'Environments'

    $expired = @(Get-BcEnvironment -DockerExecutable $DockerExecutable | Where-Object { $_.Expired })
    if ($expired.Count -eq 0) {
        Write-ALbuildLog 'No expired environments found.'
        return
    }

    foreach ($environment in $expired) {
        if ($PSCmdlet.ShouldProcess($environment.Name, "Remove expired environment (expired $($environment.ExpiresAt))")) {
            Remove-BcEnvironment -Name $environment.Name -TraefikConfigFolder $TraefikConfigFolder -DockerExecutable $DockerExecutable -Confirm:$false
            $environment.Name
        }
    }
}