How to manually modify the state file

Last updated: September 9, 2025

In some cases, especially when encountering errors like "Index value required" or when managing complex resource addresses, you may need to make adjustments to the state. There are two main approaches available depending on your use case and comfort level:


Option 1: Use State Commands in Spacelift Tasks

If you only need to remove or modify specific resources in the state, consider running the appropriate state commands (e.g., tofu state rm, terraform state mv etc.) via a Spacelift Task:

  • Run the command inside a Task to inspect or modify the current state.

  • Use terraform state rm to remove problematic or obsolete resources.

This approach is ideal for targeted and minimal changes.


Option 2: Export, Modify, and Re-import the State File

If command-line operations aren't sufficient (e.g., complex JSON edits are required), you can:

  1. Export the current state file
    Use the "Export" button from the State History section or terraform state pull.

  2. Make your changes to the state.json file
    Edit carefully to modify or remove problematic entries.

  3. Lock the stack
    This ensures no concurrent runs can interfere while you're working with state.

  4. Re-import the modified state
    Choose one of the following:

    • Upload using the "Import state" button.

    • Mount the file and run terraform state push via a Task.

Important: Always lock the stack before pushing modified state to avoid conflicts with other operations.


Best Practices

  • Backup the current state before making any modifications.

  • Avoid editing state unless necessary-it's safer and more predictable to use Terraform’s native lifecycle where possible.