# atmos terraform clean

Use this command to clean up Terraform files for an Atmos component in a stack. This removes `.terraform` folder, `.terraform.lock.hcl` file, and Atmos-generated varfiles and planfiles.

## Usage

Execute the `terraform clean` command like this:

```shell
atmos terraform clean <component> -s <stack> [--skip-lock-file] [--everything] [--force] [--cache]
```

:::warning
The `clean` command, by default, deletes all Terraform-related files, including local state files, but will prompt for confirmation before proceeding. Using the `--force` flag skips the confirmation prompt and executes the deletion immediately.
Use these flags with extreme caution as they can lead to irreversible data loss.
:::

:::tip
Run `atmos terraform clean --help` to see all the available options
:::

## Configuration

Configure default behavior for `terraform clean` in your `atmos.yaml`:

```yaml
components:
  terraform:
    # Auto-generate backend configuration
    auto_generate_backend_file: true
```

The Terraform data directory location can be controlled via environment variable:

```shell
# Set custom Terraform data directory (defaults to .terraform)
export TF_DATA_DIR="/tmp/.terraform"
```

**Configure Terraform**

Learn how to configure Terraform components in your `atmos.yaml`, including backends, auto-generation, and initialization options.

## Examples

```shell
# Delete all Terraform-related files for all components (with confirmation)
atmos terraform clean
# Force delete all Terraform-related files for all components (no confirmation)
atmos terraform clean --force
atmos terraform clean top-level-component1 -s tenant1-ue2-dev
atmos terraform clean infra/vpc -s tenant1-ue2-staging
atmos terraform clean infra/vpc -s tenant1-ue2-staging --skip-lock-file
atmos terraform clean test/test-component -s tenant1-ue2-dev
atmos terraform clean test/test-component-override-2 -s tenant2-ue2-prod
atmos terraform clean test/test-component-override-3 -s tenant1-ue2-dev
# Clean the shared plugin cache directory
atmos terraform clean --cache
# Clean both component files and plugin cache
atmos terraform clean --cache --force
```

## Arguments

- **`component` (optional)**

  Atmos terraform component. If not specified along with `--force`, cleans all components.

## Flags

- **`--stack` (alias `-s`) (optional)**

  Atmos stack. Required when specifying a component.
- **`--dry-run` (optional)**

  Dry run. Shows what files would be deleted without actually deleting them.
  ```shell
  atmos terraform clean <component> -s <stack> --dry-run
  ```
- **`--everything` (optional)**

  If set, Atmos will also delete the Terraform state files and directories for the component (the `terraform.tfstate.d` directory).
  ```shell
  atmos terraform clean <component> -s <stack> --everything
  ```
- **`--force` (alias `-f`) (optional)**

  Forcefully delete Terraform state files and directories without prompting for confirmation.
  ```shell
  atmos terraform clean <component> -s <stack> --force
  ```
- **`--skip-lock-file` (optional)**

  Skip deleting the `.terraform.lock.hcl` file.
- **`--cache` (optional)**

  Clean the shared Terraform plugin cache directory (`~/.cache/atmos/terraform/plugins` by default). This removes all cached provider plugins that are shared across components.
  ```shell
  atmos terraform clean --cache
  atmos terraform clean --cache --force
  ```
  Use this to free disk space or force re-downloading of providers.

## Related Commands

- [terraform init](/cli/commands/terraform/init) - Initialize a Terraform working directory
- [terraform plan](/cli/commands/terraform/plan) - Generate an execution plan
- [terraform apply](/cli/commands/terraform/apply) - Apply changes to infrastructure
