Clean up snapshots

Prev Next

Synopsis

This script deletes all the snapshots that are older than the number of days specified.

Sample script


[CmdletBinding()]
Param
(

	[Parameter(Mandatory = $true)]
	[URI]$uri,

	# Delete snapshots after the specified days.
	[Parameter(Mandatory = $true)]
	[int]$removeOlderThan = 100,

	[Parameter()]
	[string]$username = "",

	[Parameter()]
	[string]$password = ""

)

# Set credentials
if ($username -eq "")
{
	$credentials = Get-Credential
}
else
{
	$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, (ConvertTo-SecureString -String $password -AsPlainText -Force)
}

# Connect to Application Workspace.
$context = Connect-LiquitWorkspace -URI $uri -Credential $credentials;

# Get all packages.
$packages = Get-LiquitPackage;

# Remove after x number of days.
$removeAfter = [DateTime]::Today.AddDays(-$removeOlderThan);



# Loop through the packages.
foreach ($package in $packages) {

	# Get all snapshots.
	$snapshots = $package | Get-LiquitPackageSnapshot

	# Loop through the snapshots.
	foreach ($snapshot in $snapshots) {

		# Skip active snapshots and sandboxes.
		if (($snapshot.Type -eq "Development") -or ($snapshot.Type -eq "Test") -or ($snapshot.Type -eq "Acceptance") -or ($snapshot.Type -eq "Production")) {
				continue;
		}

		# Verify age.
		if ($removeAfter -le $snapshot.CreatedAt) {
				continue;
		}

		Write-Host "Removing snapshot on package ""$($package.Name)"" with name ""$($snapshot.Name)""."
        try
        {
		# Remove snapshot.
		$snapshot | Remove-LiquitPackageSnapshot 
        }
        catch
        {
            if (($_.Exception).Code -eq "Authorization_RequestDenied"){
                Write-Host "Insufficient privileges to delete the snapshot on package " "$($package.Name)"" with name ""$($snapshot.Name)""."
            }
        }
        
	}

}

Downloads

Script

This sample is not supported by Recast Software. It was written by a community member and is published "as is." It seems to have worked for at least one person and might work for you. But please make sure to test, test, test before you do anything drastic with it.