Public/resource.ps1

# Cleanup of already-expunged resources: permanently removes the leftover
# database rows for resources CloudStack has already destroyed and expunged.

function Remove-CSExpungedResource {
    <#
    .SYNOPSIS
        Purges the database records of already-expunged resources.

    .DESCRIPTION
        Wraps purgeExpungedResources. When a resource such as a VM is destroyed and
        expunged, CloudStack keeps a removed row in its database; this permanently
        deletes those rows to keep the database small. It only touches resources
        that are already expunged, so running VMs and recoverable ones are never
        affected, but the purged history cannot be recovered afterwards. This is an
        asynchronous job; use -Wait to block until it finishes and return the count
        purged.

    .PARAMETER ResourceType
        The type of expunged resource to purge. Currently only VirtualMachine is
        supported, which is also the default.

    .PARAMETER BatchSize
        Delete records in batches of this size to limit database load

    .PARAMETER StartDate
        Only purge resources expunged on or after this date. A 'yyyy-MM-dd' (or
        'yyyy-MM-dd HH:mm:ss') string or a DateTime.

    .PARAMETER EndDate
        Only purge resources expunged on or before this date. A string or DateTime,
        as for -StartDate.

    .PARAMETER Wait
        Wait for the async job to finish and return the number of resources purged

    .EXAMPLE
        Remove-CSExpungedResource -Wait
        Purges every expunged VM record and reports how many were removed.

    .EXAMPLE
        Remove-CSExpungedResource -EndDate (Get-Date).AddMonths(-6) -Wait
        Purges only records for VMs expunged more than six months ago.

    .EXAMPLE
        Remove-CSExpungedResource -StartDate '2026-01-01' -EndDate '2026-06-30' -BatchSize 100
        Purges a date range in batches of 100, returning the job handle immediately.
    #>

    [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')]
    param(
        [ValidateSet('VirtualMachine')]
        [string]$ResourceType,

        [int]$BatchSize,

        [object]$StartDate,

        [object]$EndDate,

        [switch]$Wait
    )

    $apiParams = @{}
    if ($PSBoundParameters.ContainsKey('ResourceType')) { $apiParams['resourcetype'] = $ResourceType }
    if ($PSBoundParameters.ContainsKey('BatchSize')) { $apiParams['batchsize'] = $BatchSize }
    if ($PSBoundParameters.ContainsKey('StartDate')) { $apiParams['startdate'] = ConvertTo-CSDateParameter -Value $StartDate -Format 'yyyy-MM-dd HH:mm:ss' }
    if ($PSBoundParameters.ContainsKey('EndDate')) { $apiParams['enddate'] = ConvertTo-CSDateParameter -Value $EndDate -Format 'yyyy-MM-dd HH:mm:ss' }

    $range = if ($apiParams.startdate -or $apiParams.enddate) { " expunged $($apiParams.startdate) to $($apiParams.enddate)" } else { '' }
    if ($PSCmdlet.ShouldProcess("expunged resource records$range", 'Purge')) {
        Invoke-CSAsyncApiRequest -Command 'purgeExpungedResources' -Parameters $apiParams -Wait:$Wait
    }
}