Delete a variable from an specific Library Set using REST

Hi Team,
We have a requirement to delete a variable from a specific library set. I did find some methods but I wanted to be sure that this change will not affect any other Variables or library set. Could you please share me the PowerShell Script which can be used to delete an specific variable from Library set and using REST API Calls. Please let me know if you need any other details.

Sujesh Sasidharan

Hi Sujesh,

Thanks for getting in touch!

The general REST API process your script would need to run would be:

  • Retrieve the Library Variable set variables object
  • Delete the desired variable from the entire object
  • Perform a PUT with the updated variable set object

We have a sample script that is designed to update values within a library variable set that would be a good starting point for you.

This would return the specific variable object and you would just need to amend the script to remove that object or create a new object minus that one.

$ErrorActionPreference = "Stop";

# Define working variables
$octopusURL = "xxxx"
$octopusAPIKey = "xxxxx"
$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey }

# Specify the Space to search in
$spaceName = "xxxx"

# Library Variable Set
$libraryVariableSetName = "xxxx"

# Variable name to search for
$VariableName = "xxxx"

$space = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/spaces/all" -Headers $header) | Where-Object {$_.Name -eq $spaceName}

Write-Host "Looking for library variable set '$libraryVariableSet'"
$LibraryvariableSets = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($space.Id)/libraryvariablesets?contentType=Variables" -Headers $header)
$LibraryVariableSet = $LibraryVariableSets.Items | Where-Object { $_.Name -eq $libraryVariableSetName }

if ($null -eq $libraryVariableSet) {
    Write-Warning "Library variable set not found with name '$libraryVariableSetName'."

$LibraryVariableSetVariables = (Invoke-RestMethod -Method Get -Uri "$OctopusURL/api/$($Space.Id)/variables/$($LibraryVariableSet.VariableSetId)" -Headers $Header) 

$existingVariable = $LibraryVariableSetVariables.Variables  | Where-Object {$ -eq $VariableName}

$UpdatedLibraryVariableSet = Invoke-RestMethod -Method Put -Uri "$OctopusURL/api/$($Space.Id)/variables/$($LibraryVariableSetVariables.Id)" -Headers $Header -Body ($LibraryVariableSetVariables | ConvertTo-Json -Depth 10)


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