How to cancel an ongoing release apart from manually doing it

Hi Team,

I have a requirement where there is a condition, if the the condition is satisfied the deployment will be succeeded or else the deployment will be failed.

I am using a powershell script to fail a release by using “THROW” command.

But instead of failing the release i need to CANCEL the release

I know that there is an option to change the release state using below script:
$header = @{ “X-Octopus-ApiKey” = $apikey }
$body = @{ State = ‘Failed’; Reason = ‘Cos I said so’; } | ConvertTo-Json
Invoke-WebRequest “” -Method POST -Headers $header -Body $body

but this works only for already created releases, but my requirement is to “CANCEL” an ongoing release through API or scripts instead of failing it.

As there is an option to cancel the ongoing release manually then there might be a backend script which is doing it.

Can you please help me with that particular script or API for cancelling the release if my condition is not satisfied.


$A=gc env.txt | select -index 0
if ($A -eq “Yes”)
echo “proceeding to next task”
throw “failed, as env is on freeze”

By this script my deployment will failed if condition is not satisfied, but i need the release to be cancelled.

It would be of great help if a solution to this problem is provided.

Shaik Sadiq

Hi Shaik,

Thanks for getting in touch! I think I have a fairly simple solution here for you.

Assuming you know the ServerTask-ID, you can run something like the following script:

# You can this dll from your Octopus Server/Tentacle installation directory or from
Add-Type -Path 'C:\MyScripts\Octopus.Client.dll' 

$apikey = 'API-KEY' # Get this from your profile
$octopusURI = 'http://OctopusServer' # Your server address

$endpoint = new-object Octopus.Client.OctopusServerEndpoint $octopusURI,$apikey 
$repository = new-object Octopus.Client.OctopusRepository $endpoint

$deployments = $repository.Tasks.get("ServerTasks-5583")


With this command, you can supply the ID for the server task which you would like to cancel, then pass it through $repository.tasks.Cancel() which should immediately cancel the task.

Does this sound like it’s what you are after?

Let me know if this helps, or if you have any further questions here.

Best regards,

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