Fixing Terraform locking state error

Remote states are usefull in terraform as they allow collaboration in a project. However sometimes we end up locking a state by mistake. Here is how you can unlock it.

Clearing a state

The most common case when a state gets incorrectly locked is when someone presses a ctrl+c while terraform plan or terraform apply is running. Especially if the gracefull quitting is interrupted. This result in the error:

    Error locking state: Error aquiring the state lock: ...

This will usually be following by the lock ID.

It's possible to run the terraform plan and apply commands with the option -lock=false. But this get tyring fast. And the lock error will possibly also appear for your teammates. The better way to solve this specific situation is to run the following command:

    terraform force-unlock LOCK_ID

Of course make sure this is not a legit lock.