Bulk delete of hosts/environments

What is the easiest way I can script a bulk delete of hosts?
I have about 100 machines that need to be removed and manually just won’t do.
Ideally if I could delete an entire environment at a time would be great.
I am on version 3.7.10

Hi @eswUser, apologies for the delay in our response to you.

Probably the easiest way would be to use the Octopus.Client library to loop over all machines and remove them. Here is a really basic script to get you started:

const string server = "https://host/"; //url of your server
const string apiKey = "API-KEY-GOES-HERE"; // Get this from your 'profile' page in the Octopus web portal
var endpoint = new OctopusServerEndpoint(server, apiKey);
var repository = new OctopusRepository(endpoint);
foreach(var m in repository.Machines.FindAll().Where(m => false /* set your predicate up here*/)) {

If you need to remove the environment as well as the machines too, that would require a couple of additional lines, but looks similar to the code above, just use the repository.Environments property to find it and delete it.

It is worth mentioning that a machine can participate in many environments, so for instance you would potentially need to consider scenarios where machines pointed to both ‘Dev’ & ‘Prod’ (but possibly not, it depends on your setup)

Hope that helps!

Thanks for that Jim. Yes that will do just fine.

No problem, Happy Deployments :slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.