Terraform Destroy

In the previous blog, we create a terraform resource we can delete it using terraform destroy command.

Terraform Destroy

The terraform destroy is used to destroy infrastructure governed by terraform. To check the behavior of terraform destroycommand at any time we can use terraform plan -destroy command.

let's use terraform destroy command to delete the infra that we created in the last blog.

┌──(gaurav㉿learning-ocean)-[~/terraform/youtube-course/terraform-first-resource]
└─$ terraform destroy
github_repository.terraform-first-repo: Refreshing state... [id=first-repo-from-terraform]
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  - destroy
Terraform will perform the following actions:
  # github_repository.terraform-first-repo will be destroyed
  - resource "github_repository" "terraform-first-repo" {
      - allow_auto_merge       = false -> null
      - allow_merge_commit     = true -> null
      - allow_rebase_merge     = true -> null
      - allow_squash_merge     = true -> null
      - archived               = false -> null
      - auto_init              = true -> null
      - branches               = [
          - {
              - name      = "main"
              - protected = false
            },
        ] -> null
      - default_branch         = "main" -> null
      - delete_branch_on_merge = false -> null
      - description            = "My First resource for my youtube viewers." -> null
      - etag                   = "W/**f09b3b6b2064f4478f01a77d22ac102992fb35bc660e**" -> null
      - full_name              = "XXXXXXXX/first-repo-from-terraform" -> null
      - git_clone_url          = "git://github.com/XXXXXXXX/first-repo-from-terraform.git" -> null
      - has_downloads          = false -> null
      - has_issues             = false -> null
      - has_projects           = false -> null
      - has_wiki               = false -> null
      - html_url               = "https://github.com/XXXXXXXX/first-repo-from-terraform" -> null
      - http_clone_url         = "https://github.com/XXXXXXXXv/first-repo-from-terraform.git" -> null
      - id                     = "first-repo-from-terraform" -> null
      - is_template            = false -> null
      - name                   = "first-repo-from-terraform" -> null
      - node_id                = "R_kgDOHehMxxhw" -> null
      - private                = false -> null
      - repo_id                = 501763207 -> null
      - ssh_clone_url          = "[email protected]:XXXXXXXXX/first-repo-from-terraform.git" -> null
      - svn_url                = "https://github.com/XXXXXXXX/first-repo-from-terraform" -> null
      - visibility             = "public" -> null
      - vulnerability_alerts   = false -> null
    }
Plan: 0 to add, 0 to change, 1 to destroy.
Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.
  Enter a value: yes
github_repository.terraform-first-repo: Destroying... [id=first-repo-from-terraform]
github_repository.terraform-first-repo: Destruction complete after 0s
Destroy complete! Resources: 1 destroyed.

For the deletion of the particular resource and its dependencies -targetflag can be used

example:

┌──(gaurav㉿learning-ocean)-[~/terraform/youtube-course/terraform-first-resource]
└─$ terraform destroy --target github_repository.terraform-second-repo

Demo Video