API to delete multiple offline agents from Azure DevOps

3/1/2019

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.

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

-- dparkar
azure-devops
azure-devops-rest-api
azure-devops-self-hosted-agent
kubernetes

2 Answers

3/1/2019

I think you would need to use a combination of these 2 api calls:

  1. Get deployment groups
  2. Delete agent
-- 4c74356b41
Source: StackOverflow

3/2/2019
$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
    }

Some assumptions for this solution:

  1. You are looking for build agents
  2. 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
  3. You don't have any issues deleting any offline agents

Note: I'm using Azure DevOps Server, so replace the -UseDefaultCredentials with your authorization.

-- Matt
Source: StackOverflow