Terraform Tutorial — Workspace Commands

less than 1 minute read

Terraform provides a set of workspace subcommands to manage workspaces.


workspace list

Show all workspaces (current marked with *):

$ terraform workspace list
  default
* dev
  staging
  prod

workspace show

Display the current workspace:

$ terraform workspace show
dev

workspace new

Create a new workspace and switch to it:

$ terraform workspace new test
Created and switched to workspace "test"!

workspace select

Switch to an existing workspace:

$ terraform workspace select prod
Switched to workspace "prod".

workspace delete

Delete a workspace (must not have resources):

# First destroy resources in the workspace
$ terraform workspace select test
$ terraform destroy -auto-approve

# Then switch away and delete
$ terraform workspace select default
$ terraform workspace delete test
Deleted workspace "test"!

⚠️ Warning: You cannot delete a workspace that still has resources in its state. Run terraform destroy first.


Command Summary

Command Purpose
workspace list List all workspaces
workspace show Show current workspace
workspace new NAME Create and switch to new workspace
workspace select NAME Switch to existing workspace
workspace delete NAME Delete empty workspace

What’s Next?

Let’s use the workspace name inside configuration to differentiate resources.


← What Are Workspaces? Workspace Interpolation →