Azure DevOps – Undelete a Release Pipeline

A few months ago I deleted a folder with inside a release definition.
My friend Alessandro Alpi helped me a lot pointing me to this msdn article.
With the PowerShell script that you find following the link you can restore a deleted release pipeline but only within 4 weeks of the deletion.

I write down the PowerShell script here for future memory in case the MSDN blog post is moved.

param (
[string] $token
## Construct a basic auth head using PAT
function BasicAuthHeader()
$ba = (":{0}" -f $authtoken)
$ba = [System.Text.Encoding]::UTF8.GetBytes($ba)
$ba = [System.Convert]::ToBase64String($ba)
$h = @{Authorization=("Basic{0}" -f $ba);ContentType="application/json"}
return $h

# Fill in your account name/project name/definition name
$accountName = "aseemb"
$projectName = "dockerConf"
$definitionNameToRecover = "New Release Definition"
# Find the Id of release definition that got deleted
$deletedReleaseDefinitionsUri = "https://$$projectName/_apis/Release/definitions?api-version=4.0-preview.3&isDeleted=true&searchText=$definitionNameToRecover"
$h = BasicAuthHeader $token
$deletedDefinitions = Invoke-RestMethod -Uri $deletedReleaseDefinitionsUri -Headers $h -ContentType “application/json" -Method Get
$deletedDefinitionJSON = $deletedDefinitions | ConvertTo-Json -Depth 100
write-host "Found the deleted definitions : $deletedDefinitions"
$deletedReleaseDefinitionId = $deletedDefinitions.Value[0].id
write-host "Found the deleted id : $deletedReleaseDefinitionId " # Recover the deleted release definition
$undeleteReason = '{ "Comment" : "Deleted by mistake" }'
$undeleteReleaseDefinitionUri = "https://$$projectName/_apis/Release/definitions/$deletedReleaseDefinitionId`?api-version=4.0-preview.3"
$undeletedDefinition = Invoke-RestMethod -Uri $undeleteReleaseDefinitionUri -Headers $h -ContentType “application/json" -Method Patch -Body $undeleteReason
$name = $
write-host "$name recovered successfully"

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.