Apply retention policies - recursive variable error

FYI, I modified the script provided to delete releases. Thought I’d post it in case it helps anyone else out:

param(
    $OctopusURL = "https://localhost",
    [parameter(Mandatory=$true)][SecureString]$SecurePassword,
    $KeepDates = @{
        dev = (Get-Date).AddDays(-90)
        release = (Get-Date).AddDays(-90)
        ci = (Get-Date).AddDays(-90)
        mac = (Get-Date).AddDays(-90)
        united = (Get-Date).AddDays(-90)
    }
)

$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList "Domain\User", $SecurePassword
$octopusAPIKey = $Credentials.GetNetworkCredential().Password
$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey }

$ErrorActionPreference = "Stop";

#$spaceName = "default"

# Get space
#$space = (Invoke-RestMethod -Method Get -Uri "$OctopusURL/api/spaces/all" -Headers $header -SkipCertificateCheck) | Where-Object {$_.Name -eq $spaceName}
$channels = Invoke-RestMethod -Method Get -Uri "$OctopusURL/api/channels/all" -Headers $header -SkipCertificateCheck
# Get project
$projects = Invoke-RestMethod -Method Get -Uri "$OctopusURL/api/$($space.Id)/projects/all" -Headers $header -SkipCertificateCheck

foreach ($project in $projects) {
    $chs = $channels | Where-Object { $_.ProjectId -eq $project.Id }
    #$project = $projects | Where-Object {$_.Name -eq $projectName}

    foreach ($ch in $chs) {
        Write-Host "Project [$($project.Name)] Channel [$($ch.slug)] - Getting releases"
        # Get releases for project
        #$releases = Invoke-RestMethod -Method Get -Uri "$OctopusURL/api/$($space.Id)/projects/$($project.Id)/releases?take=1000" -Headers $header -SkipCertificateCheck
        $releases = Invoke-RestMethod -Method Get -Uri "$OctopusURL/api/channels/$($ch.Id)/releases?take=1000" -Headers $header -SkipCertificateCheck

        # Loop through list
        if ($releases -and $releases.Items) {
            foreach ($release in $releases.Items)
            {
                $keepDate = $keepDates[$ch.Slug]
                if ($release -and $release.Assembled -lt $keepDate) {
                    Write-Host "Project [$($project.Name)] Channel [$($ch.slug)] - Deleting Release [$($release.Version)]"
                    # Delete release
                    Invoke-RestMethod -Method Delete -Uri "$OctopusURL/api/$($space.Id)/releases/$($release.Id)" -Headers $header -SkipCertificateCheck   
                }
            }
        }
    }
}
1 Like

Hey @josh_centerx,

Great news you managed to modify one of our scripts to delete the releases. A word of caution for any user that wants to use this. The script should error out if that release is associated with anything inside Octopus but make sure you backup your databases before running any scripts on your Octopus Instance just in case anything goes wrong.

Thanks for posting that up Josh!

Kind Regards,

Clare

1 Like

Could you suggest how to detect that the release is associated with anything? The original script didn’t have anything like that either.

Hey @josh_centerx,

I don’t believe that script has any association checks in place. It just clears releases for the provided project.

Best,
Brent

Right. I’m asking what APIs I should call to make sure I don’t corrupt things when clearing releases?

Hi Josh,

Thanks for following up. Deleting releases like this via the API will be okay and cascade-delete any dependent resources attached to the release being deleted, the same as if you were to delete them via the UI. You shouldn’t have to worry at all about any corruption happening while deleting releases via this endpoint. The only time it should be problematic is if you were trying to delete the releases straight in the database.

I hope that helps, don’t hesitate to reach out with any questions at all moving forward.

Best regards,

Kenny

OK, thanks. I was just a bit concerned about borking our DB after Clare’s comment & wanted to be duly careful.

2 Likes

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.