title: Azure Pipeline runs not automatically deleting
body: <p>We have two build YAML pipelines to run tests on TFS one for front-end and one for back-end tests. We use two self-hosted windows agents to run these builds and we are on Azure DevOps Server 2020. Ever since we started running the pipeline we noticed our TFS database size ballooning up. We've tried editing our retention settings to only keep the most recent builds but they are still saved no matter what we change.</p>
  <p>Below are our retention settings:</p>
  <p><strong>Collection Settings</strong></p>
  <p><a href="https://i.stack.imgur.com/5n6DY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5n6DY.png" alt="enter image description here" /></a></p>
  <p><strong>Project Pipeline Settings</strong></p>
  <p><a href="https://i.stack.imgur.com/WaN6O.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WaN6O.png" alt="enter image description here" /></a></p>
  <p><strong>Project Pipeline Release Retention:</strong></p>
  <p><a href="https://i.stack.imgur.com/qFtlC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qFtlC.png" alt="enter image description here" /></a></p>
  <p><strong>EDIT: Test Retention Settings:</strong></p>
  <p><a href="https://i.stack.imgur.com/eBuVs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eBuVs.png" alt="enter image description here" /></a></p>
  <p><strong>END EDIT</strong></p>
  <p>Our YAML pipelines don't have any specific retention settings so my understanding would be that it defaults to the project pipeline settings. However, this doesn't seem to be the case. We have runs that go as far back as November, which is when we first created the pipelines. I can also manually check which builds are and aren't retained:</p>
  <p><strong>Example of retained build</strong>
  <a href="https://i.stack.imgur.com/JvvXl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JvvXl.png" alt="enter image description here" /></a>
  <a href="https://i.stack.imgur.com/hB6aZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hB6aZ.png" alt="enter image description here" /></a></p>
  <p><strong>Example of not retained build</strong>
  <a href="https://i.stack.imgur.com/LSgk3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LSgk3.png" alt="enter image description here" /></a>
  <a href="https://i.stack.imgur.com/RQxGO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RQxGO.png" alt="enter image description here" /></a></p>
  <p>However, these runs are just never getting deleted. Is my understanding of how retention works just incorrect and the builds shouldn't be getting automatically deleted by TFS? Or do I need to change my Azure DevOps settings somehow to automatically delete the builds once the retention settings no longer apply to it?</p>
  <p>It's also worth mentioning we do have a release pipeline, although we've been experiencing these problems since way before we created it and the release pipeline only has dependencies on master but no feature runs are getting deleted either.</p>
  <p>I did find <a href="https://stackoverflow.com/questions/59970547/azure-devops-server-2019-retention-policy-no-longer-working/60320828#60320828">this</a> SO post that seems to only apply to classic UI pipeline editor. Is there a way I can apply this to my YAML pipeline?</p>
  <p><strong>Edit 2</strong></p>
  <p>So I did make some more progress on this. I wrote a script that would delete all the runs that weren't held by retention settings:</p>
  <pre><code>$token = &quot;{PAT}&quot;
  $url=&quot;https://{instance}/{collection}/{project}/_apis/build/builds/?api-version=6.0&quot;
  $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(&quot;:$($token)&quot;))
  
  $response = Invoke-RestMethod -Uri $url -Headers @{Authorization = &quot;Basic $token&quot;} -Method Get -ContentType application/json
  
  ForEach( $build in $response.value )
  {
      if ((-not $build.keepForever) -and (-not $build.retainedByRelease)) {
          $url=&quot;https://{instance}/{collection}/{project}/_apis/build/builds/$($build.id)?api-version=6.0&quot;
          $deleteResponse = Invoke-RestMethod -Uri $url -Headers @{Authorization = &quot;Basic $token&quot;} -Method Delete -ContentType application/json
          Write-Host &quot;Deleted&quot; $url
      }
  } 
  </code></pre>
  <p>This deletes all the runs just fine. However, from what I can tell in the docs, this should happen automatically by TFS. Are there any event log sources I could filter for or any specific times of day to check that may lead me to where the actual deletion of the runs should be taking place?</p>