Is there a way to remove multiple build agents in one shot

I had a rouge process that went off and created a whole bunch of agents that I dont want.  Now in my unauthorized list of agents I need to remove alot of them.  Do I have to go thru each one and click remove agent or is there a way to do this in bulk?

3
2 comments

I assume this has yet to be implemented….  we just had a rogue process create 22,700+ unauthorized agents and deleting them one at a time by had is just not reasonable to expect anyone to do.

0

Hi, 

Please refer to the Agent API documentation - https://www.jetbrains.com/help/teamcity/rest/manage-agents.html#Delete+Build+Agent.

You can use the list API to retrieve the agents and the delete API to remove the agents using your custom scripts. The following is the demo code with PowerShell. How to get the access token, please refer to  https://www.jetbrains.com/help/teamcity/rest/quick-start.html#Authentication

# TeamCity server URL and credentials
$teamCityServerUrl = "http://your-teamcity-server-url:port“ # You need to change it to your server url. e.g.: http://localhost:8111
$teamCityUsername = "your-username"
$teamCityAccessToken= "your-accesToken”

# Base64 encode the credentials for Basic Auth
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($teamCityUsername):$($teamCityAccessToken)"))

# Get all agents
$agentsUrl = "$teamCityServerUrl/app/rest/agents"
$agentsResponse = Invoke-RestMethod -Uri $agentsUrl -Headers @{
    Authorization = ("Basic {0}" -f $base64AuthInfo)
    Accept = "application/json"
} -Method Get

# Iterate over each agent and delete
foreach ($agent in $agentsResponse.agent) {
    $agentId = $agent.id
    $deleteAgentUrl = "$teamCityServerUrl/app/rest/agents/id:$agentId"
    Invoke-RestMethod -Uri $deleteAgentUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method Delete
    Write-Output "Deleted agent with ID: $agentId"
}

Write-Output "All agents (except default agent) have been deleted."
0

Please sign in to leave a comment.