Skip to content

Instantly share code, notes, and snippets.

@jakenuts
Created January 22, 2024 06:12
Show Gist options
  • Save jakenuts/c642b14ea9b94763866a2f0c6afa1f98 to your computer and use it in GitHub Desktop.
Save jakenuts/c642b14ea9b94763866a2f0c6afa1f98 to your computer and use it in GitHub Desktop.
Delete Old Azure DevOps Builds (One or more builds associated with the requested pipeline(s) are retained by a release. The pipeline(s) and builds will not be deleted)
# https://tenbulls.co.uk/2020/03/25/delete-old-build-definitions-in-azure-devops/
$personalToken = "askjxlkasjxlkasjxsajkxj"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($personalToken)"))
$header = @{authorization = "Basic $token"}
$organization = "retracement"
$project = "ACME%20Corp"
#all build definitions
$url = "https://dev.azure.com/$organization/$project/_apis/build/definitions?api-version=6.0-preview.7"
$builddefinitions = Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers $header
$builddefinitions.value | where {$_.name -eq "ARM Templates Build"}|Sort-Object id|ForEach-Object {
Write-Host $_.id $_.name $_.queueStatus
#all builds for a definition
$url = "https://dev.azure.com/$organization/$project/_apis/build/builds?definitions=" + $_.id + "&api-version=6.0-preview.5"
$builds = Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers $header
$builds.value | Sort-Object id|ForEach-Object {
#report on retain status
Write-Host " BuildId" $_.id "- retainedByRelease:" $_.retainedByRelease
#api call for a build
$url = "https://dev.azure.com/$organization/$project/_apis/build/builds/" + $_.id + "?api-version=6.0-preview.5"
#set retainedByRelease property to false
Invoke-RestMethod -Uri $url -Method Patch -Body (ConvertTo-Json @{"retainedByRelease"='false'}) -ContentType "application/json" -Headers $header
}
Write-Host
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment