tftidy
A Go tool that recursively scans Terraform files and removes transient blocks: moved, removed, and import.
Overview
tftidy unifies these tools into a single CLI:
It helps clean up Terraform configurations after refactors and state operations by removing no-longer-needed transition blocks while preserving the rest of the file.
Features
- Recursively scans directories for
.tf files
- Removes selected block types:
moved, removed, import
- Supports selecting block types with
--type
- Supports dry-run mode (
--dry-run)
- Preserves original file permissions on write
- Skips writes when no target blocks are found
- Applies HCL formatting after removal
- Optional whitespace normalization (
--normalize-whitespace)
- Reports detailed processing statistics
Requirements
Installation
From Source
git clone https://github.com/mkusaka/tftidy.git
cd tftidy
go build -o tftidy ./cmd/tftidy
Using Go Install
go install github.com/mkusaka/tftidy/cmd/tftidy@latest
This installs the binary into your $GOPATH/bin (or $GOBIN if set).
From GitHub Releases
Download pre-built binaries from the Releases page.
Available platforms: linux/amd64, linux/arm64, darwin/amd64, darwin/arm64, windows/amd64.
Usage
tftidy [options] [directory]
If directory is not specified, the current directory is used.
Options
-t, --type string
Comma-separated block types to remove.
Valid values: moved, removed, import, all.
Default: moved,removed,import
-n, --dry-run
Preview changes without modifying files.
-v, --verbose
Show each file being processed.
--remove-comments
Also remove leading comments attached to removed blocks.
--normalize-whitespace
Normalize consecutive blank lines after removal.
--version
Show version.
-h, --help
Show help.
Examples
Remove all supported block types in the current directory:
tftidy
Remove only moved blocks:
tftidy --type moved ./terraform
Remove moved and import blocks with whitespace normalization:
tftidy --type moved,import --normalize-whitespace ./terraform
Preview only (no file writes):
tftidy --dry-run ./terraform
GitHub Actions
You can use tftidy as a GitHub Action in your workflows.
Note: This action installs the tftidy binary and adds it to PATH. It does not run tftidy automatically — you need to invoke it in a subsequent step. The action downloads a pre-built binary from GitHub Releases. A release must exist before the action can be used. The gh CLI and bash are required on the runner (pre-installed on all GitHub-hosted runners).
Basic Usage
- uses: mkusaka/tftidy@v0
- run: tftidy
With Options
- uses: mkusaka/tftidy@v0
with:
version: "v1.2.3"
- run: tftidy --type moved,import --verbose ./terraform
| Input |
Description |
Default |
version |
Version of tftidy to install. Use latest or a tag like v1.2.3. |
latest |
Example Workflow
name: Terraform Tidy
on:
pull_request:
paths:
- "**/*.tf"
jobs:
tftidy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: mkusaka/tftidy@v0
- run: tftidy --type moved,removed,import --verbose
Example Files
Legacy example sets were migrated into this repository under examples/.
examples/moved/
examples/removed/
examples/import/
See examples/README.md for a quick walkthrough.
Command migration paths
From terraform-moved-remover:
terraform-moved-remover [options] [directory]
# ->
tftidy --type moved [options] [directory]
From terraform-removed-remover:
terraform-removed-remover [options] [directory]
# ->
tftidy --type removed [options] [directory]
From terraform-import-remover:
terraform-import-remover [options] [directory]
# ->
tftidy --type import [options] [directory]
Option mapping
-dry-run / --dry-run -> -n / --dry-run
-verbose / --verbose -> -v / --verbose
-normalize-whitespace / --normalize-whitespace -> --normalize-whitespace
-help / --help -> -h / --help
-version / --version -> --version
Behavioral differences
- Files are not rewritten when no target blocks are found (no format-only writes).
- Original file permissions are preserved when files are rewritten.
- Exit codes are strict:
1 for runtime/file processing errors
2 for usage/argument errors
Example Output
Files processed: 15
Files modified: 7
Files errored: 0
Blocks removed:
moved: 12
removed: 3
import: 5
total: 20
Exit Codes
0: success
1: runtime/file processing error(s)
2: usage/argument error
If processing errors occur, tftidy continues other files and returns 1 at the end.
How It Works
tftidy parses Terraform files using HashiCorp HCL v2, locates target top-level blocks, removes their byte ranges, formats the result, and writes changes in place (unless dry-run).
File discovery is powered by github.com/boyter/gocodewalker, and excludes .terraform / .terragrunt-cache directories.
Development
go build -v ./cmd/tftidy
go test -v ./...
go test -race ./...
License
MIT