1

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
  • 1,934
  • 2
  • 22
  • 52
  • related : https://stackoverflow.com/questions/54206874/azure-devops-rest-api-get-builds-currently-queued-in-agent-pool – dparkar Mar 01 '19 at 04:02

3 Answers3

1
$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
  • 3,658
  • 3
  • 14
  • 27
0

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

  1. Get deployment groups
  2. Delete agent
4c74356b41
  • 69,186
  • 6
  • 100
  • 141
0

Or you can use some naming matrix for agents, so they are using the same "name" even if it is not the same container.

https://github.com/julioct/azure-pipelines-kubernetes-agents

FN_
  • 715
  • 9
  • 27