Terraform downloads providers into every project folder
Provider binaries are hundreds of megabytes each and each project gets its own. The plugin cache setting that stops it.
Running terraform init downloads the providers a configuration needs into a .terraform folder inside that project. Do that in ten repositories and you have ten copies of the same binaries, each several hundred megabytes.
Find every copy
Get-ChildItem $env:USERPROFILE -Recurse -Directory -Filter .terraform -Depth 5 -Force -EA SilentlyContinue |
ForEach-Object {
$s = (Get-ChildItem $_.FullName -Recurse -Force -File -EA SilentlyContinue | Measure-Object Length -Sum).Sum
[PSCustomObject]@{ MB = [math]::Round($s/1MB); Path = $_.FullName }
} | Sort-Object MB -Descending | Select-Object -First 20Read that for repetition rather than size. The same provider version in eight projects is the problem, and it has a one line fix that applies from then on.
The plugin cache, on Windows
$cache = "$env:APPDATA\terraform.d\plugin-cache"
New-Item -ItemType Directory -Force -Path $cache | Out-Null
Set-Content "$env:APPDATA\terraform.rc" "plugin_cache_dir = `"$($cache -replace '\\','/')`""Terraform reads terraform.rc from %APPDATA% on Windows rather than .terraformrc in the home directory, which is the detail that makes most guides fail here. With it set, providers are downloaded once and linked from each project.
Cleaning up what already exists
- Set the plugin cache first, or you will do this again next month.
- Delete
.terraformfolders in projects you are not applying. - Run
terraform initagain when you return to one. - Leave
terraform.tfstatealone entirely.
The one thing not to delete
State files. terraform.tfstate and its backups describe what actually exists in your infrastructure, and for a project using local state they are not recoverable from anything on the machine. The .terraform folder is downloads; the state file is a record.
The neighbours
An infrastructure machine usually also has container images and Kubernetes tooling, which are larger again. Docker Desktop on Windows covers the biggest of them.
Common questions
Where does Terraform store providers on Windows?
In a .terraform folder inside each project by default. A shared plugin cache is configured in %APPDATA%\terraform.rc, which is the Windows equivalent of .terraformrc.
How do I set up a Terraform plugin cache on Windows?
Create a folder such as %APPDATA%\terraform.d\plugin-cache and set plugin_cache_dir to it in %APPDATA%\terraform.rc. Subsequent inits link to the shared copy.
Is it safe to delete .terraform folders?
Yes. They hold downloaded providers and modules, restored by running terraform init. Never delete terraform.tfstate files, which record the real state of your infrastructure.
How much space do Terraform providers use?
Hundreds of megabytes per provider version, and a project using several can exceed a gigabyte. Multiplied across projects without a shared cache, it is a large hidden folder.