{"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/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/state_show.go#L106-L142","documentation":"Returned by `terraform state show` when, after refreshing the backend state manager, `stateMgr.State()` returns nil — i.e. there is no state file at all for the current workspace. State-management commands require an existing state, so this is a hard stop with guidance to run in an initialized directory or point `-state` at a file.","triggerScenarios":"`stateMgr.RefreshState()` succeeds but `state == nil`: the configured backend has no state object (empty workspace, never-applied directory, or `-state` points at a non-existent path).","commonSituations":"Running `terraform state show <addr>` in a fresh project that has never been applied; wrong workspace selected; `-state=...` path is wrong; backend state object was deleted out-of-band.","solutions":["Run `terraform apply` first to create initial state, then `terraform state show`.","Verify the workspace with `terraform workspace list`/`show` and switch to the one that holds the resource.","Check the `-state` flag path or the backend configuration (e.g. S3 bucket/key) is correct."],"exampleFix":"# before\nterraform state show aws_instance.web   # fresh dir -> error\n# after\nterraform apply && terraform state show aws_instance.web","handlingStrategy":"validation","validationCode":"// Mirror the check before operating on state:\nstate := stateMgr.State()\nif state == nil {\n    return errors.New(errStateNotFound)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Confirm an apply has created state before running state commands.","Verify the workspace and `-state` path before `terraform state show`.","In automation, check `terraform state list` exit status before deeper queries."],"tags":["state","state-show","no-state","backend","terraform-cli"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}