SQL Clone 2

Purge old images which don't have clones

If you are regularly creating images on a schedule, you may wish to clear out the older ones. This script will remove images older than a given date, as long as they don't have clone databases dependent on them (in which case warn in the output).

This script will remove images without any further warning. You will need to test this and add protections appropriate to your environment before live use.

Connect-SqlClone -ServerUrl 'http://sql-clone.example.com:14145'

$imageTimeToLiveDays = 7;

$oldImages = Get-SqlCloneImage | Where-Object {$_.CreatedDate -le (Get-Date).AddDays(0-$imageTimeToLiveDays)}

foreach ($image in $oldImages)
{
    $clones = Get-SqlClone -Image $image
    
    if (!($clones -eq $null))
    {
        "Will not remove image {0} because it has {1} dependent clone(s)." -f $image.Name, $clones.Count
    }
    else
    {
        Remove-SqlCloneImage -Image $image
        "Removed image {0}." -f $image.Name
    }
}

Didn't find what you were looking for?