Resolution
Find any orphaned references to a single variable set via the API that aren't surfaced on the usage tab. Any snapshot returned from the script needs to be deleted so the variable set deletion can succeed.
Symptom
Even when a variable set's usage tab is empty, the following error can still be surfaced when trying to delete it.
The variable set can't be deleted because it's currently being referenced in a release or runbook snapshot. Please refer to usages for more detail.
Scope: when this is the right article
This article applies to variable sets, accounts and certificates that cannot be deleted in the UI and show no usages.
Cause
Sometimes references to a variable set can become orphaned in a way that is not surfaced on the variable set's usage, while also preventing its deletion.
This can either be due to an orphaned snapshot in the dbo.VariableSet table in your Octopus database, or there are references to it in existent release and runbook snapshots that aren't surfaced in the variable set's usage tab.
It may have been caused as a result of bug 9834 with missing validation during a POST to the api/<Space-Id>/runbookSnapshots endpoint. If you created a runbook snapshot via the API, and specified a different project ID than what the runbook is in, this can cause data integrity issues leading to this same result.
Diagnosis and fix
If the deletion fails due to being referenced by a variable, the easiest way to track it down is to query your Octopus SQL database. Please carefully review the following SQL, and ensure you have a database backup prior to running any commands against your database.
You can identify the orphaned variable set snapshot via SELECT * FROM VariableSet WHERE Id like '%LibraryVariableSets-XX%'. This will return variable set snapshots, suffixed with an ID (for example, variableset-LibraryVariableSets-123-s-2-EHKO3). These are what we'd need to target for deletion via DELETE * FROM VariableSet WHERE Id = 'variableset-LibraryVariableSets-123-s-2-EHKO3'.
If a variable set's usage tab in the UI is empty, but the deletion still fails due to a reference to it in a release or runbook snapshot, we'll need to track down the orphaned snapshots. Again, the easiest way to track these down would be to run the following queries against your SQL database.
Query to find references in the RunbookSnapshot table: SELECT * FROM RunbookSnapshot where JSON like '%LibraryVariableSets-XX%'
Query to find references in the Release table: SELECT * FROM Release where JSON like '%LibraryVariableSets-XX%'
If anything is returned, you can manually find them in the UI for deletion.
Alternatively, you can identify any unsurfaced references to this variable set via an API script, plugging in your instance-specific values (URL, API key, SpaceId and LibraryVariableSetId). This scans through every individual project in the space, and returns any reference to the variable set in any of its release or runbook snapshots.
$ErrorActionPreference = "Stop";
# Prompted Parameters
$OctopusAPIKey = ''
$OctopusUrl = ''
$SpaceId = ''
$LibraryVariableSetId = ''
$Header = @{ "X-Octopus-ApiKey" = $OctopusAPIKey }
# Get usage for specified Library Variable Set
Try {
$Usage = Invoke-RestMethod -Method Get -Uri "$OctopusUrl/api/$($SpaceId)/libraryvariablesets/$($LibraryVariableSetId)/usages" -Headers $Header
}
Catch {
Write-Warning "Something went wrong when attempting a GET for `"$($OctopusUrl)/api/$($SpaceId)/libraryvariablesets/$($LibraryVariableSetId)/usages`""
Write-Warning "Check your parameters (Octopus API key, URL, SpaceId, LibraryVariableSetId) and ensure your API key has sufficient permissions."
break
}
$SnapshotDelCount = 0
$ProjReleaseDelCount = 0
# Confirmation
Write-Host ""
Write-Host "======================================================================================="
Write-Host "Please ensure you have taken a fresh backup of your Octopus database before continuing."
Write-Warning "This process may not be reversible without a database backup."
Write-Host "Found: $($Usage.Projects.RunbookSnapshots.SnapshotId.Count) Runbook Snapshots"
Write-Host "Found: $($Usage.Projects.Releases.ReleaseId.Count) Project Releases"
$Continue = Read-Host "Would you like to continue with deletion? (Type Y or N)"
Write-Host "======================================================================================="
Write-Host ""
While (($Continue -ne "N") -and ($Continue -ne "Y")) {
Write-Host "Please ensure you have taken a fresh backup of your Octopus database before continuing."
Write-Warning "This process is not reversible without a database backup."
Write-Host "Found: $($Usage.Projects.RunbookSnapshots.SnapshotId.Count) Runbook Snapshots"
Write-Host "Found: $($Usage.Projects.Releases.ReleaseId.Count) Project Releases"
$Continue = Read-Host "Would you like to continue with deletion? (Type Y or N)"
}
If ($Continue -eq "Y") {
# Delete list of Runbook Snapshots associated with the Library Variable Set
$RunbookSnapshots = $Usage.Projects.RunbookSnapshots.SnapshotId
foreach ($RunbookSnapshot in $RunbookSnapshots) {
Try {
Invoke-RestMethod -Method Delete -Uri "$OctopusUrl/api/$($SpaceId)/runbooksnapshots/$($RunbookSnapshot)" -Headers $Header
}
Catch {
Write-Warning "Unable to delete Runbook Snapshot: $RunbookSnapshot"
}
Write-Host "$RunbookSnapshot deleted"
$SnapshotDelCount++
}
# Delete list of Project Releases associated with the Library Variable Set
$ProjectReleases = $Usage.Projects.Releases.ReleaseId
foreach ($ProjectRelease in $ProjectReleases) {
Try {
Invoke-RestMethod -Method Delete -Uri "$OctopusUrl/api/$($SpaceId)/releases/$($ProjectRelease)" -Headers $Header
}
Catch {
Write-Warning "Unable to delete Project Release: $ProjectRelease"
}
Write-Host "$ProjectRelease deleted"
$ProjReleaseDelCount++
}
}
# Summary
Write-Host ""
Write-Host "==="
Write-Host "The operation has completed"
Write-Host "$SnapshotDelCount out of $($Usage.Projects.RunbookSnapshots.SnapshotId.Count) Runbook Snapshots were deleted"
Write-Host "$ProjReleaseDelCount out of $($Usage.Projects.Releases.ReleaseId.Count) Project Releases were deleted"
Write-Host "==="
Write-Host ""
$CheckSuccess = Read-Host 'Did this work as expected? (Type Y or N)'
While (($CheckSuccess -ne "N") -and ($CheckSuccess -ne "Y")) {
$CheckSuccess = Read-Host 'Did this work as expected? (Type Y or N)'
}
If ($CheckSuccess -eq "N") {
Write-Host "To restore, stop the Octopus Server service for your instance. Restore your SQL db backup. Then, restart the Octopus Server service."
}
Verify
After you have identified any additional snapshots that reference this variable set from running this script, you can approve the deletion of those snapshots during script execution, or navigate to them in the UI and delete them manually. After all of these snapshots are deleted, you should then be able to delete the variable set.
If this API-route does not return any results, and you're still prevented from deleting a variable set in this way, reach out to our support team.
Related
Attempting to delete other objects that are captured by release and runbook snapshots, like certificates and accounts, can also be impacted.
Related GitHub issues:
https://github.com/OctopusDeploy/Issues/issues/9330
https://github.com/OctopusDeploy/Issues/issues/5890
https://github.com/OctopusDeploy/Issues/issues/8226
https://github.com/OctopusDeploy/Issues/issues/6932