Remove Environments with No Machines

Lee C's Avatar

Lee C

05 Dec, 2017 05:09 AM

Hi

Is there a way I can clean up Octopus to remove any environments which haven't got a machine in? I have a few hundred environments which had machines in but they have been removed due to being offline for more than x days. The problem is, we now have lots of environments with no machines in !

Thanks - Lee

  1. Support Staff 1 Posted by Dalmiro Grañas on 05 Dec, 2017 03:25 PM

    Dalmiro Grañas's Avatar

    Hi Lee,

    Thanks for reaching out. The only way to do this would be using our API. I recommend you to take a look at Octoposh which has a few cmdlets that'll help you automating some tasks by using our API.

    The below code snippets should help you delete all empty environments:

    long version

    $AllEnvironments = Get-OctopusEnvironment


    $EnvironmentsToDelete = $AllEnvironments | ?{$_.Machines.Count -eq 0}


    foreach($environment in $EnvironmentsToDelete){ "About to delete $($environment.name)" #Remove-OctopusResource -Resource $environment.resource #Uncomment this line if you want the environments to be deleted }

    one-liner

    Get-OctopusEnvironment | ? {$_.Machines.count -eq 0} |Remove-OctopusResource
    

    Hope that helps,
    Dalmiro

  2. 2 Posted by Lee C on 06 Dec, 2017 01:06 AM

    Lee C's Avatar

    Thanks, that worked a treat.

    Is there any documentation for any housekeeping we can do? It has been very unloved and loading the dashboard is very slow. I have just cleaned over 500 environments which were no longer in use!

  3. Support Staff 3 Posted by Dalmiro Grañas on 07 Dec, 2017 03:22 PM

    Dalmiro Grañas's Avatar

    Hi Lee,

    I'm glad to hear that worked.

    As for documentation about housekeeping, I'm afraid we don't have such a specific doc as housekeeping may vary a lot depending on each use case. Some users like you might have dynamic infrastructure and might need a script to delete empty environments; some other users might consider that keeping more than 10 releases is too much, so they want to make use of Retention Policies. There's a few tips I can give you though:

    • Check our Triggers feature: With it you could have a project dedicated to different cleanup tasks that gets automatically triggered whenever a trigger gets called.

    • Rely on our REST API: As you probably know already, our API will help you query anything in your instance and then delete things based on your own criteria. Of course this will implicate some custom scripting on your side. You can check our Swagger support to learn how to use the API.

    • Check Octoposh: While its not a project that we officially maintain, lots of our users use their cmdlets to do things like getting a list of Octopus resources, filtering them based on a property and then deleting them. Just like the example I showed you in my previous reply.

    If you are interested in a specific kind of cleanup, let me know and I could point you in the right direction.

    Best regards,
    Dalmiro

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac