<p>Our Azure DevOps build agents are setup on Kubernetes. Failed pods can easily be deleted from kube, but they appear as "offline" agents from the Azure DevOps Web UI. </p>

<p>Overtime the list of offline agents has grown really long. Is there a way to programmatically delete them ?</p>

dparkar
<pre><code>$agents = Invoke-RestMethod -uri 'http://dev.azure.com/{organization}/_apis/distributedtask/pools/29/agents' -Method Get -UseDefaultCredentials
$agents.value |
    Where-Object { $_.status -eq 'offline' } |
    ForEach-Object {
        Invoke-RestMethod -uri "http://dev.azure.com/{organization}/_apis/distributedtask/pools/29/agents/$($_.id)?api-version=4.1" -Method Delete -UseDefaultCredentials
    }
</code></pre>

<p>Some assumptions for this solution:</p>

<ol>
<li>You are looking for build agents</li>
<li>You know the id of the pool you are looking for already.  You can get to this programatically also, or just loop through all pools if you want</li>
<li>You don't have any issues deleting any offline agents</li>
</ol>

<p><strong>Note:</strong>  I'm using Azure DevOps Server, so replace the <code>-UseDefaultCredentials</code> with your authorization.</p>
