Delete or Amend a channel in bulk

Hi,

We’ve created a script using your example which creates a channel and adds it to a list of projects using a foreach loop. How would I go about delete the channel if I wanted to rollback the change. Creating a diff script I can use a get call what parameters would I need for the delete. I’ve tried changing the method to delete but to no avail

Define working variables

$octopusURL = “XXXX”
$octopusAPIKey = “XXXX”
$header = @{ “X-Octopus-ApiKey” = $octopusAPIKey }
$spaceName = “Default”
$projectNames = “XXXXX”
$channelName = “XXXX”

Get space

$spaces = Invoke-RestMethod -Uri “$octopusURL/api/spaces?partialName=$([uri]::EscapeDataString($spaceName))&skip=0&take=100” -Headers $header
$space = $spaces.Items | Where-Object { $_.Name -eq $spaceName }

(Invoke-RestMethod -Method Get -Uri “$octopusURL/api/$($space.Id)/channels/all” -Headers $header) | Where-Object {$_.Name -eq $channelName}

Thanks,

Dan

Hi @daniel.witter,

Thanks for reaching out to Octopus Support and for the question!

It looks as though you’re on the right track but if you add a channel to each project individually and wish to roll this back, you will need to individually delete the channel from each project.

Inside the foreach loop, each project name should be used to find the project ID of the project and then that can be used to delete the channel from the project directly.

The URL to delete a channel from within the project is $octopusUrl/api/projects/projects-id/channels/channels-id

$ErrorActionPreference = "Stop";

# Define working variables
$octopusURL = "https://octopusURL"
$octopusAPIKey = "API-XXXXXXXXXXXXXXXXXXXXXXXXX"
$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey }
$spaceName = ""
$projectNames = @("", "")
$channelName = ""

# Find space
$spaces = Invoke-RestMethod -Uri "$octopusURL/api/spaces?partialName=$([uri]::EscapeDataString($spaceName))&skip=0&take=100" -Headers $header 
$space = $spaces.Items | Where-Object { $_.Name -eq $spaceName }

# Get all projects
$projects = Invoke-RestMethod -Uri "$octopusURL/api/$($space.Id)/projects/all" -headers $header

# foreach listed project in $projectNames
foreach($project in $projectNames){

    # Find project by name
    $projectData = $projects | Where-Object { $_.Name -eq $project }

    # Find channel by name
    $channels = Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($space.Id)/projects/$($projectData.Id)/channels" -Headers $header
    $channel = $channels.Items | Where-Object { $_.Name -eq $channelName }

    # Delete channel  
    Invoke-RestMethod -Method Delete -Uri ("$octopusURL" + "$($channel.Links.Self)") -Headers $header
}

I hope this helps! Please let me know if you’re still running into issues.

Kind Regards,
Adam

Thanks Adam that worked a treat

1 Like

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