How do I exec a script before destroy instance with terraform?

I'm need to exec a script into instance before delete it.

For example:

resource "linode_instance" "gitlab-runner" {
  image           = "linode/ubuntu22.04"
  count           = var.runners_count
  label           = "runner-${count.index}"
  group           = "Terraform"
  region          = "us-central"
  private_ip      = false
  type            = "g6-standard-2"
  tags            = ["cluster: runners", "docker", "terraform"]

  provisioner "file" {
    source      = ""
    destination = "/tmp/"

  provisioner "remote-exec" {
    when = destroy
    inline = [
      "chmod +x /tmp/",
      "/tmp/ args",


1 Reply

Hi @vinicius_souza - I think the code you have as an example won't work for what you have in mind. There's an official guide on remote-exec provisioners in Terraform that may help:

The code example in the guide looks identical to the one you provided, when it needs to be specific to things you've set up within your Linode in order to run as intended.


Please enter an answer

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct