Private/Clear-KadenExpiredTransaction.ps1

function Clear-KadenExpiredTransaction {
    <#
    .SYNOPSIS
        Remove completed transaction journals whose retention window has elapsed.
    .DESCRIPTION
        Incomplete journals, staging, and backups stay until an explicit repair completes them.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [string]$ChildRoot
    )

    $txnRoot = Join-Path $ChildRoot (Join-Path '.kaden' 'transactions')
    if (-not (Test-Path -LiteralPath $txnRoot)) {
        return
    }

    $now = [datetime]::UtcNow
    foreach ($dir in @(Get-ChildItem -LiteralPath $txnRoot -Directory -ErrorAction SilentlyContinue)) {
        $journalPath = Join-Path $dir.FullName 'journal.json'
        if (-not (Test-Path -LiteralPath $journalPath)) {
            continue
        }
        try {
            $doc = Get-Content -LiteralPath $journalPath -Raw | ConvertFrom-Json
        }
        catch {
            continue
        }
        if ([string]$doc.status -ne 'completed') {
            continue
        }
        if ([string]::IsNullOrWhiteSpace([string]$doc.cleanupEligibleAtUtc)) {
            continue
        }
        $eligible = [datetime]::Parse([string]$doc.cleanupEligibleAtUtc).ToUniversalTime()
        if ($eligible -lt $now) {
            Remove-Item -LiteralPath $dir.FullName -Recurse -Force
        }
    }
}