Skip to main content
This lesson focuses on two essential lifecycle operations in Terraform: updating existing infrastructure to match changed requirements, and safely removing resources when they are no longer needed. Real environments change regularly—Terraform models the desired end state and produces safe, predictable plans to reconcile your cloud resources with that state. Below you’ll find clear examples showing:
  • an initial Azure resource group configuration,
  • how Terraform plans and applies an in-place update when you add tags,
  • how Terraform shows a destroy plan and performs destruction,
  • how multiple resources interact during updates and removals,
  • and what the state file looks like after a full destroy.
For further reading, see the Terraform documentation: https://www.terraform.io/docs and the Azure Provider docs: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
Terraform models desired state rather than imperative commands. Always run terraform plan before terraform apply to review what will change, and keep your state file backed up or versioned.

Initial configuration

Here is a minimal Terraform configuration that configures the Azure provider and creates one resource group.
When you run terraform apply the first time, Terraform compares this configuration to the current Azure state. Since the resource group does not yet exist, Terraform will create it.

Updating resources (adding tags)

Suppose you modify the same resource group to include tags. Terraform will compare the desired state (your HCL) with the current state and plan an in-place update if the provider supports updating the attribute without replacement. Updated configuration with a tags block:
Running terraform plan will show the detected change as an in-place update. The tilde (~) symbol means “update in-place”. Example plan output:
The plan clearly indicates which attribute will change (Owner tag) and confirms the resource will be modified in-place instead of being replaced.

Quick reference: plan symbols

Note: Whether an attribute change causes an in-place update or a full replacement depends on the provider and the specific attribute. For details check the Azure provider docs.

Destroying resources

To remove managed resources from Azure, use terraform destroy. Terraform will build a destroy plan showing resources that will be removed; a minus (-) symbol denotes destruction. If the entire resource is scheduled for removal, the minus applies to the resource block; if just an attribute is removed from the configuration, the minus may apply only to that attribute. Example interactive terraform destroy output:
Destroy is destructive and irreversible through Terraform. Always review the plan carefully and ensure you have backups or snapshots if required.

Working with multiple resources

Real-world Terraform configurations typically manage many resources together. The example below shows a storage account and a virtual network managed alongside a resource group.
Applying these resources:
If you later remove the storage account block from your configuration and update resource group tags, terraform plan will present both an in-place update and a planned destroy for the storage account:
If your goal is simply to remove all resources managed in state without editing the configuration, terraform destroy will remove everything after confirmation.

State file after a full destroy

After successfully destroying all managed resources, Terraform updates the state file. A state file showing no managed resources will have an empty resources array, for example:
You can also verify in the Azure portal that the resource group and its related resources have been removed.
The image shows a Microsoft Azure portal page displaying a list of resource groups, along with their corresponding subscriptions and locations. The interface includes options to create, refresh, export, and filter the resource groups.

Watch Video