Issues icon indicating copy to clipboard operation
Issues copied to clipboard

504 error may occur when attempting to delete an Environment

Open donnybell opened this issue 1 year ago • 0 comments

Severity

No response

Version

Probably all through 2023.4

Latest Version

None

What happened?

If there are many Deployments referencing an Environment that a user is attempting to delete, this may result in a 504 error.

Reproduction

Reproduction is likely difficult without a database with real-world usage as this seems to happen organically.

Error and Stacktrace

No response

More Information

Report #1 (Internal Link) Report #2 (Internal Link)

Workaround

This PowerShell script finds and deletes all Deployments associated with a script to create more breathing room for the delete Environment request via the UI.

As a precaution, line 21 is commented out, which performs the delete. Once you are comfortable with the script and are ready to run it, simple remove the # at the beginning of line 21. In an abundance of caution, we recommend taking a SQL db backup before running the script below as that would be your only way to restore the Deployments that are deleted.

# This script will find and delete all Deployments associated with an Environment in a given Space

$ErrorActionPreference = "Stop";

# Define working variables
$OctopusURL = "https://YOUR_OCTOPUS_URL" # replace YOUR_OCTOPUS_URL with the target Octopus instance url (i.e. myserver.com)
$octopusAPIKey = "API-XXXX" # replace API-XXXX with a valid API key for your Octopus instance
$Header = @{ "X-Octopus-ApiKey" = $octopusAPIKey }
$SpaceId = "Spaces-XX" # replace Spaces-XX with the target Space Id
$EnvironmentName = "ENV_NAME_HERE" # replace ENV_NAME_HERE with the exact name of the target Environment

# Find Environment
$Environment = (Invoke-RestMethod -Method GET "$OctopusURL/api/$($SpaceId)/Environments/all" -Headers $Header) | Where-Object {$_.Name -eq $EnvironmentName}

# Get list of Deployments
$DeploymentsList = Invoke-RestMethod -Method GET "$OctopusURL/api/$($SpaceId)/Deployments?take=1000000&environments=$($Environment.Id)" -Headers $Header
$Deployments = $DeploymentsList.items

# Delete Deployments
foreach ($Deployment in $Deployments) {
    #Invoke-RestMethod -Method Delete -Uri "$OctopusURL/api/$($SpaceId)/Deployments/$($Deployment.Id)" -Headers $header
    Write-Host "$($Deployment.Id) deleted" 
}
Write-Host "---"
Write-Host "$($Deployments.count) total Deployments deleted"

donnybell avatar Mar 11 '24 15:03 donnybell