Cleanup Azure DevOps pipelines retained by releases

Published on 22 December 2021

One situation I've come across a while ago was not being able to remove some deprecated pipelines due to the following error:

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

Going through the builds REST documentation, I was able to check that Builds do have a property "retainedByRelease". Not only that, but digging further and looking at the leases REST documentation I also noticed a boolean property named "protectPipeline" that, according to its very relevant description states that:

If set, this lease will also prevent the pipeline from being deleted while the lease is still valid.

Now I was finally getting somewhere, and I've attempted to patch the lease and set that property to false. Surprised, or maybe not, when you set that property to false for all the leases associated with the build, then the build own property "retainedByRelease" also turns out to false, allowing you to finally delete it permanently!

To automate things, since I had multiple builds with the same state, I've created the below python script, that you are invited to read and use. At the time I was using Fiddler to check the requests, but you can remove the "fiddler_proxies" related stuff from there if you don't need that.

Please follow the instructions on how to create a Personal Access Token and encode it using a base64 encoding tool like this one.

Make sure to fill in the "project_url", the "encoded_pat" and the "build_definition_ids" variables, before running the script. You can get the build IDs from the URL when you enter a build definition using the DevOps portal

The project url can also be get from the address bar, and for this script it needs to be in the format:{org name}/{project name}.

comments powered by Disqus