6

I'm trying to delete an Azure DevOps build definition but it won't let me since it says:

"One or more builds associated with the requested pipeline(s) are retained by a release. The pipeline(s) and builds will not be deleted."

However there's no obvious way to see what release is causing a build to be retained. I tried searching online, of course, but all the examples/screenshots of how to do this in the web UI are showing UI from several iterations ago of the Azure DevOps website so none of the controls look the same anymore. I don't see a lock icon anywhere, for example.

How can I find the releases that are holding onto these build definitions so I can delete them and then delete the build definition?

Thanks!

Auth Infant
  • 1,760
  • 1
  • 16
  • 34

2 Answers2

5

When you open the build pipeline to see its detailed build records, you can see the relevant release name and its link:

enter image description here

On old pipeline version, there had a lock icon which can obvious let us know it is retained. In fact, the lock icon not only means it is retained by release, manual build retain also show this icon. But, seems we missed this obvious icon while we expand the new sprint.

As a workaround to get list of builds which retained by release, here has a short script can help you achieve by using Rest api:

$token = "{PAT token}"

$url ="https://dev.azure.com/{org name}/{project name}/_apis/build/builds?api-version=5.1"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))

$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get

$results = $response.value | Where {$_.retainedByRelease -eq "true"} #|

Write-Host "results = $($results.id | ConvertTo-Json -Depth 100)"

First, use list builds api to get all builds that in current project. Then for the builds which retained by release, since there has a parameter can indicate it: retainedByRelease here I use $_.retainedByRelease -eq "true" to get the builds list which actual retained by release:

enter image description here

The above script is very universal, can be used in Powershell-ise and Powershell Command Line and the Powershell task of VSTS without change anything.

Update in 11/19:

Based on @Auth's comment, if want to get its associated release, the most easiest way is find the build, and then check its associate release pipeline as the screenshot shown I shared above.

If this does not satisfied what you want, and the previous API we used does not include any releases info in that, so here, you need use this API: Releases - Get Release:

GET https://vsrm.dev.azure.com/{org name}/{project name}/_apis/release/releases?sourceId={project id}:{build definition id}&api-version=5.1

In this API, you need specified the project id:build definition id to filter releases.

enter image description here


With the icon missing, will let the corresponding team know and try to add it in the future sprint.

Mengdi Liang
  • 17,577
  • 2
  • 28
  • 35
  • Thank you for the explanation and the script! I was able to modify your script to more specifically help with my scenario -- deleting a specific build definition -- by adding a "$_definition.id" filter to the Where clause. Unfortunately, that only gets me part way to a solution. Now I can see which builds are retained by release, but how do I find out which release it is that is causing them to be retained? AFAICT, the retainedByRelease property is just a boolean, not something that tells you which release id is causing the build to be retained. – Auth Infant Nov 18 '19 at 17:33
  • @AuthInfant There's no any release info listed in this API, to get the associate releases, you need apply another API to filter that. See my updated content. In fact, for me, it's more convenient to back to UI and search the builds we got previously. Then find out the releases from UI which shown as my first screenshots shared. Because after all, we need delete something by using UI. – Mengdi Liang Nov 19 '19 at 02:33
  • Thank you for the screenshot pointer. Unfortunately, I do not see what you see. For example, by running the script you gave me, I can see that build ID 4119 has retainedByRelease set to true, but if I bring up the details for that build in the web UI, there is no "Releases" tab on the page only a "Summary" tab (even though the web page shows the "Retained by release" text on it). The same is true for all the other builds from this definition that have retainedByRelease set to true. – Auth Infant Nov 19 '19 at 06:17
  • @AuthInfant, It's so wired. So now, you had to use above powershell scripts to achieve associated release pipeline. Feel free to let me know if you need any assistance about this. Hope the updated script could indeed help. – Mengdi Liang Nov 19 '19 at 07:21
  • Thanks for the continued suggestions. I tried using the REST API to query for all releases associated with the build definition that cannot be deleted but, unfortunately, the REST query shows that there are zero releases associated with that build definition. I verified that my release REST query was valid because I tried it for a definition I knew had releases and it properly listed them. For these build definitions I'm trying to delete, they are "retained by release" but do not appear to actually have any releases for them according to the REST API and the UI. How could this happen? – Auth Infant Nov 19 '19 at 18:35
  • @AuthInfant If this, I may understand why there’s no release displayed on the your Release tab which like pic I shared above. But it’s so hard for me to face. – Mengdi Liang Nov 19 '19 at 18:45
  • I can't explain how this state occurs but since I knew there was no release for this build definition anymore and I really wanted to delete it, I ended up following your lead with regards to just using the REST API and used a PATCH command to just set the retainedByRelease property to false for all the builds from that definition, then I was able to successfully delete the build definition. – Auth Infant Nov 19 '19 at 22:21
4

I have had this issue when some one delete a repository that was associated with a "POC" pipeline. I was not able to delete the pipeline.

When I tried to delete the pipeline, I got following message:

One or more builds associated with the requested pipeline(s) are retained by a release. The pipeline(s) and builds will not be deleted.

enter image description here

I did following steps to delete the pipeline:

  1. My Goal was to delete PublishBuildArtifacts-Infrastructure pipeline as shown in image. So I clicked on the pipeline.

enter image description here

  1. I see list of Builds (Runs) that is associated with my pipeline. I click on the 3 dots and click on View Retention Lease.

enter image description here

  1. Then I click Remove all.

enter image description here

  1. Now you can delete your build.

enter image description here

Repeat steps 2-4 for each run. When you have delete all builds, now you can delete the pipeline by clicking on the 3 dot menu, as shown in the image below.

enter image description here

Enjoy!

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137