Skip to content

Support for running a script on workspace delete #13387

Description

@evilhamsterman

Right now there's support to run scripts on workspace startup and stop, but it would also be nice to run a script when a workspace is deleted.

For example we have workspaces that are persistent VMs, when they are started they install an register an XDR agent. We'd like to run a script to uninstall the agent which also unregisters it when they are deleted. We can run a script to do so when the workspace is stopped, but then we have to reinstall it on start and uninstall it on stop, and that creates a lot of unnecessary churn.

Activity

  1. kylecarbs commented on May 28, 2024

    @kylecarbs
    Member

    You can do this via Terraform.

    Use a null_resource and check if data.coder_workspace.me.transition === "delete", then run a script.

  2. added
    waiting-for-infoThe issue creator is asked to provide more information.
    and removed
    customer-requestedDO NOT USE. Instead, add to the project and fill in "Customer".
    on May 28, 2024
  3. evilhamsterman commented on May 29, 2024

    @evilhamsterman
    Author

    Can you run the script in the workspace like the run_on_stop option for coder_script?

  4. matifali commented on Jun 2, 2024

    @matifali
    Member

    I guess to run a script on delete transition we need an attribute run_on_delete for coder_script. It will only work if the workspace is in running state.

    @mafredri could have some thoughts here.

  5. mafredri commented on Jun 4, 2024

    @mafredri
    Member

    @matifali Yes, run_on_delete and a running workspace are prerequisites. Ideally we would also have #6175 to ensure proper execution of the script.

    Having the run_on_delete property gives a nice way for us to surface a warning if a stopped workspace is deleted and there is a run_on_delete script. Then we can suggest starting the workspace first. Whether or not this is a nice UX flow is another question. I can imagine users first stopping their workspaces before issuing delete, only to be asked to start it again.

    Alternatively, we could support this via run_on_stop by setting an environment variable CODER_WORKSPACE_STOP_REASON=stop|delete and letting the template author handle it.

  6. removed
    waiting-for-infoThe issue creator is asked to provide more information.
    on Aug 20, 2024
  7. matifali commented on Aug 20, 2024

    @matifali
    Member

    This could also be used to backup workspace contents for compliance/security purposed.

  8. wSedlacek commented on Oct 9, 2024

    @wSedlacek

    We use coder to create namespaces with permissions for devs.
    Devs can add resources to that namespace while they are running.
    But since there is no hook when the workspace is deleted and the namespace should be deleted we end up trying to delete the namespace but failing due to finalizers being blocked when not tearing down resources in a specific order.

    tl;dr A on delete solution would really help my team out. Perhaps being able to spin up a 1 off job so the deployment doesn't have to be running to complete it.

  9. matifali commented on Feb 11, 2025

    @matifali
    Member

    tl;dr A on delete solution would really help my team out. Perhaps being able to spin up a 1 off job so the deployment doesn't have to be running to complete it.

    @wSedlacek
    You should be able to run a script using local_provisioner and a null_resource and check if data.coder_workspace.me.transition === "delete".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions