Search code examples
teamcityteamcity-9.0

How to programmatically trigger cleanup?


I'm having a very busy build server. On good day we create almost 200gb of artifacts. Cleanup policy can be run once a day which is not enough for my case. I've searched the teamcity documentation and found zero API endpoints to support trigger cleanup manually.

Is it possible to trigger cleanup manually from script/program? How to achieve this?

In worst case I could fiddler up and trace what happens when i manually force cleanup, but its messy road and i don't want to go on it.

here's also C# version

void Main()
{
    var cookieContainer = new CookieContainer();
    var baseAddress = new Uri("http://teamcity");

    var contentDictionary = new Dictionary<string,string>();
    contentDictionary["cleanupPageAction"]= "startCleanup";

    using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer,
    Credentials = new NetworkCredential("user","password","domain")})
    using (var client = new HttpClient(handler) { BaseAddress = baseAddress })
    {
        var content = new FormUrlEncodedContent(contentDictionary);

        var result = client.PostAsync("/admin/cleanupPolicies.html", content).Result;
        result.EnsureSuccessStatusCode();
    }
}

Solution

  • There is no endpoint in the API for this. If you do decide you are happy enough to use the same HTTP POST the dashboard uses for a manual cleanup here it is, simple enough in my opinion:

    curl -d "cleanupPageAction=startCleanup" \
        http://user:[email protected]/admin/cleanupPolicies.html