Side effects of removing ".terraform" folder

Viewed 1629

According to the documentation, .terraform folder contains the cache for the provider's plugins.

If this folder is deleted (but the state file is kept), will it be recreated on terraform init? What are the possible unwanted effects of deleting .terraform?

3 Answers

The .terraform directory contains a few different kinds of artifact, some of which track local settings that are only relevant to your current working directory, while others are caches of data from elsewhere.

Broadly-speaking terraform init can recreate most things in the .terraform directory, but there are some small exceptions to be aware of:

  • Unless your Terraform configuration contains only local modules or has exact version constraints for remote modules, terraform init won't necessarily select the same version of each external module, if the available versions upstream have changed. The .terraform/modules/modules.json file (in current Terraform versions) tracks the specific selections in your current working directory.
  • If you've selected a non-default workspace using terraform workspace select, deleting the .terraform directory will reset back to the default workspace because Terraform uses a file under .terraform to track your current selection.
  • If you used the -backend-config option when using terraform init then deleting .terraform will discard those custom settings, and you'll need to re-specify the same settings when you run terraform init again.

The other main artifacts Terraform tracks in .terraform are derived from information outside of .terraform and so reproducible:

  • If you don't use -backend-config then your backend configuration lives in your backend block in your configuration, and so Terraform will be able to reproduce it exactly as long as you haven't changed the configuration.
  • The local provider cache in .terraform/providers is indexed by the dependency lock file, which is generated alongside rather than inside .terraform so that it can be saved in version control and be shared between working directories. terraform init will, by default, install exactly the same dependencies recorded in that file.

The above is true as of Terraform v1.0 and v1.1. The exact contents and layout of .terraform are an implementation detail of Terraform and so older versions treat it differently and newer versions may not necessarily follow exactly what I described above.

It is totally safe to delete the .terraform directory. The directory (./modules) and binaries of providers (./providers)

./
./modules           --> contains the code of used modules
./providers         --> contains the binaries of the providers
./terraform.tfstate --> the state file

As follow up to Jason's answer, the result of deletion of the terraform.tfstate file depends whether you're using a backend or not.

If you are using a backend (which is the recommended way to save your state file) the next terraform init will create a file that points to the state file.

If you delete the the .terraform folder you are correct the providers are deleted but you need to take a back up of the state file as I have seen a couple times that file sometimes gets deleted or corrupted.

A terraform init does bring .terraform folder back.

Why do you need to do this? As you can run terraform init -reconfigure and that is a safer option.

Related