hashicorp/terraform · error

schema version for in state does not match version from the…

Error message

schema version %d for %s in state does not match version %d from the provider

What it means

Thrown by jsonstate when the schema version stored on the resource instance in state (ri.Current.SchemaVersion) does not equal the schema version the currently loaded provider exposes (schema.Version). Terraform refuses to serialize JSON state from a version it cannot safely interpret, because attribute layout may differ between schema versions.

Solutions

  1. Run `terraform apply -refresh-only` (or `terraform refresh`) so the provider runs its state upgrader to the current schema version.
  2. Align the provider version in required_providers with the version that wrote the state.
  3. If the state is from a newer provider and downgrade is required, use `terraform state replace-provider` or migrate the resource to a compatible provider.
  4. Restore the state from a backup that matches the configured provider version.

Example fix

# before
required_providers { aws = { version = "~> 4.0" } }  # state written by 5.x

# after
required_providers { aws = { version = "~> 5.0" } }  # then terraform init && terraform apply -refresh-only
Defensive patterns

Strategy: validation

Validate before calling

// Compare the stored schema version against the provider's before marshaling.
if int64(ri.Current.SchemaVersion) != schema.Version {
    return fmt.Errorf("state schema version %d for %s is stale (provider has %d); run 'terraform apply -refresh-only'", ri.Current.SchemaVersion, addr, schema.Version)
}

Prevention

When it happens

Trigger: State was written by a different provider version than the one now configured; a provider upgraded its schema version but the state-upgrade step did not run (e.g. state loaded via `state pull`/`state push` without a refresh); provider downgrade.

Common situations: Downgrading a provider after state was written by a newer one; restoring a state backup from a newer Terraform/provider stack; pointing Terraform at a different provider source whose schema versioning diverged; manually edited state files.

Related errors


AI-assisted analysis of hashicorp/terraform@d32a084675 (2026-08-11). Data as JSON: /api/errors/518c4edbb587fb27. Report an issue: GitHub.

Appendix: source

Thrown at internal/command/jsonstate/state.go:421

			case addrs.DataResourceMode:
				current.Mode = DataResourceMode
			default:
				return ret, fmt.Errorf("resource %s has an unsupported mode %s",
					resAddr.String(),
					resAddr.Mode.String(),
				)
			}

			schema := schemas.ResourceTypeConfig(
				r.ProviderConfig.Provider,
				resAddr.Mode,
				resAddr.Type,
			)

			// It is possible that the only instance is deposed
			if ri.Current != nil {
				if schema.Version != int64(ri.Current.SchemaVersion) {
					return nil, fmt.Errorf("schema version %d for %s in state does not match version %d from the provider", ri.Current.SchemaVersion, resAddr, schema.Version)
				}

				current.SchemaVersion = ri.Current.SchemaVersion

				if schema.Body == nil {
					return nil, fmt.Errorf("no schema found for %s (in provider %s)", resAddr.String(), r.ProviderConfig.Provider)
				}

				// Check if we have an identity in the state
				if ri.Current.IdentityJSON != nil {
					if schema.IdentityVersion != int64(ri.Current.IdentitySchemaVersion) {
						return nil, fmt.Errorf("resource identity schema version %d for %s in state does not match version %d from the provider", ri.Current.IdentitySchemaVersion, resAddr, schema.IdentityVersion)
					}

					if schema.Identity == nil {
						return nil, fmt.Errorf("no resource identity schema found for %s (in provider %s)", resAddr.String(), r.ProviderConfig.Provider)
					}

View on GitHub (pinned to d32a084675)