Private/Invoke-CloudResourceRemoval.ps1

function Invoke-CloudResourceRemoval {
    <#
    .SYNOPSIS
        Private helper function to handle resource removal with confirmation.
     
    .DESCRIPTION
        Centralizes the ShouldProcess logic and deletion workflow for cloud resources.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [System.Management.Automation.PSCmdlet]$CallerPSCmdlet,
        
        [Parameter(Mandatory = $true)]
        [string]$ResourceType,
        
        [Parameter(Mandatory = $true)]
        [int]$ID,
        
        [Parameter(Mandatory = $true)]
        [string]$Uri,
        
        [Parameter(Mandatory = $false)]
        [scriptblock]$GetResourceScript
    )
    
    # Try to get resource details for a better confirmation message
    $resourceName = "Unnamed"
    if ($GetResourceScript) {
        try {
            $resource = & $GetResourceScript
            if ($resource -and $resource.name) {
                $resourceName = $resource.name
            }
        }
        catch {
            Write-Warning "$ResourceType with ID $ID not found."
            return
        }
        
        if (-not $resource) {
            Write-Warning "$ResourceType with ID $ID not found."
            return
        }
    }
    
    # Use the caller's ShouldProcess
    if ($CallerPSCmdlet.ShouldProcess("$ResourceType ID $ID ($resourceName)", "Remove $ResourceType")) {
        Write-Verbose "Removing $ResourceType ID ${ID}: $resourceName"
        
        try {
            $response = Invoke-CloudApiRequest -Uri $Uri -Method Delete
            Write-Host "$ResourceType $ID removed successfully." -ForegroundColor Green
            return $response
        }
        catch {
            if ($_.Exception.Message -match "not found|does not exist") {
                Write-Warning "$ResourceType $ID not found or already deleted."
                return
            }
            else {
                throw
            }
        }
    }
}