hashicorp/terraform · error

error deleting workspace %s: %v

Error message

error deleting workspace %s: %v

What it means

Delete(force) removes the workspace via Workspaces.Delete (or Workspaces.SafeDelete when supported and force is false). Any failure other than ErrResourceNotFound is wrapped here. It usually means the workspace is not in a deletable state or the token lacks permission.

Source

Thrown at internal/cloud/state.go:534

		return lockErr
	}

	return nil
}

// Delete the remote state.
func (s *State) Delete(force bool) error {
	var err error

	isSafeDeleteSupported := s.workspace.Permissions.CanForceDelete != nil
	if force || !isSafeDeleteSupported {
		err = s.tfeClient.Workspaces.Delete(context.Background(), s.organization, s.workspace.Name)
	} else {
		err = s.tfeClient.Workspaces.SafeDelete(context.Background(), s.organization, s.workspace.Name)
	}

	if err != nil && err != tfe.ErrResourceNotFound {
		return fmt.Errorf("error deleting workspace %s: %v", s.workspace.Name, err)
	}

	return nil
}

// GetRootOutputValues fetches output values from HCP Terraform
func (s *State) GetRootOutputValues(ctx context.Context) (map[string]*states.OutputValue, error) {
	// The cloud backend initializes this value to true, but we want to implement
	// some custom retry logic. This code presumes that the tfeClient doesn't need
	// to be shared with other goroutines by the caller.
	s.tfeClient.RetryServerErrors(false)
	defer s.tfeClient.RetryServerErrors(true)

	ctx, cancel := context.WithTimeout(ctx, time.Minute)
	defer cancel()

	var so *tfe.StateVersionOutputsList
	err := RetryBackoff(ctx, func() error {

View on GitHub (pinned to c9def3e214)

Solutions

  1. Pass force=true if you intend to destroy state/resources along with the workspace.
  2. Unlock the workspace before deleting it.
  3. Confirm the token has admin/delete permission on the workspace.
  4. Verify the workspace has no pending runs.
  5. Retry on a 5xx response.

Example fix

// before: safe-delete a non-empty workspace -> error deleting workspace
state.Delete(false)

// after: force-delete once you are sure state/resources should be destroyed
state.Delete(true)
Defensive patterns

Strategy: validation

Validate before calling

// Pre-flight: ensure the workspace is deletable before calling Delete
ws, err := client.Workspaces.Read(ctx, org, name)
if err == nil {
    if ws.Locked {
        return errors.New("workspace is locked; unlock before deleting")
    }
    if ws.Permissions.CanForceDelete != nil && !*ws.Permissions.CanForceDelete {
        return errors.New("token lacks delete permission on the workspace")
    }
}

Try / catch

if err := state.Delete(force); err != nil {
    if errors.Is(err, tfe.ErrResourceNotFound) {
        return nil // already gone
    }
    return err
}

Prevention

When it happens

Trigger: Deleting a workspace that still has state versions, is currently locked, has pending runs, or for which the token lacks delete permission; the API returns 4xx/5xx.

Common situations: Calling SafeDelete on a non-empty workspace (it refuses), deleting a locked workspace, or an insufficiently-scoped token.

Related errors


AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07). Data as JSON: /api/errors/4a1bd5d7ca01edb3. Report an issue: GitHub.