{"record":{"id":"c3b51ad7fb787d78","repo":"hashicorp/terraform","slug":"no-state-file-was-found-state-management-command","errorCode":null,"errorMessage":"No state file was found!\n\nState management commands require a state file. Run this command\nin a directory where Terraform has been run or use the -state flag\nto point the command to a specific state location.","messagePattern":"No state file was found!\n\nState management commands require a state file\\. Run this command\nin a directory where Terraform has been run or use the -state flag\nto point the command to a specific state location\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/state_show.go","lineNumber":124,"sourceCode":"\tenv, err := c.Workspace()\n\tif err != nil {\n\t\tdiags = diags.Append(fmt.Sprintf(\"Error selecting workspace: %s\\n\", err))\n\t\tview.Diagnostics(diags)\n\t\treturn 1\n\t}\n\tstateMgr, sDiags := b.StateMgr(env)\n\tif sDiags.HasErrors() {\n\t\tdiags = diags.Append(fmt.Errorf(errStateLoadingState, sDiags.Err()))\n\t\treturn view.DisplayResourceInstanceState(jsonformat.State{}, diags)\n\t}\n\tif err := stateMgr.RefreshState(); err != nil {\n\t\tdiags = diags.Append(fmt.Errorf(\"Failed to refresh state: %s\\n\", err))\n\t\treturn view.DisplayResourceInstanceState(jsonformat.State{}, diags)\n\t}\n\n\tstate := stateMgr.State()\n\tif state == nil {\n\t\tdiags = diags.Append(errors.New(errStateNotFound))\n\t\treturn view.DisplayResourceInstanceState(jsonformat.State{}, diags)\n\t}\n\n\tis := state.ResourceInstance(addr)\n\tif !is.HasCurrent() {\n\t\tdiags = diags.Append(errors.New(errNoInstanceFound))\n\t\treturn view.DisplayResourceInstanceState(jsonformat.State{}, diags)\n\t}\n\n\t// check if the resource has a configured provider, otherwise this will use the default provider\n\trs := state.Resource(addr.ContainingResource())\n\tabsPc := addrs.AbsProviderConfig{\n\t\tProvider: rs.ProviderConfig.Provider,\n\t\tAlias:    rs.ProviderConfig.Alias,\n\t\tModule:   addrs.RootModule,\n\t}\n\tsingleInstance := states.NewState()\n\tsingleInstance.EnsureModule(addr.Module).SetResourceInstanceCurrent(","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/state_show.go#L106-L142","documentation":"Returned by StateShowCommand (state_show.go:124) using the shared errStateNotFound constant. After refreshing state via stateMgr.RefreshState(), state := stateMgr.State() returns nil — there is no state file at all (local backend with no terraform.tfstate, or a remote backend returning empty). The message tells the operator state commands need a state file.","triggerScenarios":"Running `terraform state show <addr>` (or other state commands) in a directory where no state exists: never applied, .terraform/terraform.tfstate deleted, or the backend returned no state for the workspace.","commonSituations":"Fresh project never applied; wrong workspace selected so the backend has no state; -state flag pointing at a non-existent path; remote backend auth/network issue returned empty.","solutions":["Run `terraform apply` (or `terraform refresh`) first to create/refresh state.","Verify the workspace: `terraform workspace list` and switch to the one holding state (`terraform workspace select <name>`).","If using a custom location, pass `-state=<path>` pointing at an existing tfstate file, or fix the backend config and `terraform init`."],"exampleFix":"# before\n terraform state show aws_instance.web   # No state file was found\n# after\n terraform apply            # creates state\n terraform state show aws_instance.web","handlingStrategy":"validation","validationCode":"// Before running state commands, confirm a state file exists.\n if _, err := os.Stat(\"terraform.tfstate\"); os.IsNotExist(err) {\n     return errors.New(\"no state file; run terraform apply first\")\n }","typeGuard":null,"tryCatchPattern":"out, err := exec.Command(\"terraform\", \"state\", \"show\", addr).CombinedOutput()\n if err != nil && bytes.Contains(out, []byte(\"No state file was found\")) {\n     // run apply/refresh to create state, then retry\n }","preventionTips":["Verify the workspace holds state (`terraform state list`) before running `state show`.","Run apply/refresh to materialize state before inspecting it."],"tags":["state","state-show","no-state","backend"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}