Cert EspressoLearn. Practise. Find your flow.

A LITTLE FOCUS.
A BIG NEXT STEP.

Your pace. Your space.
HashiCorp · Terraform 004

Validate, format, and destroy

Terraform 004 · Lesson 5 of 18 · 28:30

YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗

Learning with these videos? Subscribe for new courses and exam-style questions.

In this lesson

Prepare for the HashiCorp Terraform Associate (004) exam. In this lesson: terraform validate, terraform fmt and its check and recursive options in a pipeline, and terraform destroy, with a plan in destroy mode to preview it. Includes three original practice questions, with Bluefin Logistics, a fictional parcel delivery company, as the example.

Study notes

The text of this lesson, to read at your own pace. Information as of November 2026.

Checking and cleaning up

Before Bluefin's team accepts any change, its pipeline checks the code, and at the end of a test, the team removes the whole test environment. Three commands help: one checks the configuration, one formats it, and one destroys what it manages. This lesson covers terraform validate, terraform fmt, and terraform destroy.

terraform validate

terraform validate checks that a configuration is valid: correct syntax, and internally consistent, with correct references and argument types. It never contacts remote services: it does not load remote state, and it does not call any provider API. So it cannot tell whether a resource exists, or whether your credentials work. It needs an initialized working directory, because it uses the provider schemas, and its json option gives results that other tools can process. And plan and apply also validate the configuration, before they do anything else.

terraform fmt

terraform fmt rewrites configuration files in the canonical format and style of Terraform. It sets indentation to two spaces, aligns the equals signs of nearby arguments, and fixes other layout details, without changing the meaning. By default, it only processes the files of the current directory: the recursive option adds every subdirectory. And running it before every commit keeps the code consistent, and makes reviews easier, because the diffs show real changes only.

Formatting checks in a pipeline

In a pipeline, you usually do not want files rewritten. The check option only checks the formatting: it rewrites nothing, lists the files that need formatting, and returns a non-zero exit code, which fails the pipeline. The diff option shows the changes that fmt would make. So Bluefin's pipeline runs terraform fmt with the check and recursive options, then terraform validate, on every pull request.

terraform destroy

terraform destroy deletes every object that the configuration manages, in other words, everything tracked in its state. It works in reverse order of the dependencies: a server goes before the network that it uses. It is an alias for terraform apply with the destroy option, so it shows a plan, and asks you to confirm first. To preview it without deleting anything, run terraform plan with the destroy option. And to remove a single resource, do not use destroy: delete its block from the configuration, and apply.

Recap

Let's recap. The validate command checks syntax and consistency, never contacts remote services, and needs an initialized directory. The fmt command rewrites files in the canonical style, and its recursive option adds subdirectories. In a pipeline, the check option fails the build without rewriting anything. And the destroy command removes everything in the state, in reverse order of dependencies, while deleting a block and applying removes a single resource.

Quick checks for this lesson

The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.

  1. Bluefin's pipeline runs terraform validate on every pull request, and it passes. The apply later fails, because the AWS credentials have expired. Why did validate not catch this?

    • It only checks the formatting
    • It ignores the provider blocks
    • It never contacts provider APIs
    • Expired credentials are syntax errors
    Show the answer

    It never contacts provider APIs

    Terraform validate checks the configuration itself, without contacting remote services, so it cannot test credentials. “It only checks the formatting” is a description of terraform fmt, not validate. “It ignores the provider blocks” is wrong, because validate does check provider blocks, but only their syntax and arguments. And “Expired credentials are syntax errors” is wrong, because credentials are not part of the configuration syntax: they come from the environment.

  2. Bluefin's pipeline must fail when any file, in any directory, is not formatted, without rewriting the files. Which command should it run?

    • terraform fmt -check -recursive
    • terraform fmt -recursive
    • terraform fmt -check
    • terraform validate -recursive
    Show the answer

    terraform fmt -check -recursive

    The check option fails the pipeline without rewriting anything, and the recursive option covers every subdirectory. “terraform fmt -recursive” would rewrite the files, instead of failing. “terraform fmt -check” would only check the current directory. And “terraform validate -recursive” is wrong, because validate has no recursive option, and it does not check formatting.

  3. Bluefin no longer needs one test server, but the rest of the environment must stay. What is the recommended way to remove only that server?

    • Run terraform destroy in the directory
    • Delete the server in the cloud console
    • Run terraform plan with -destroy
    • Delete its block, then apply
    Show the answer

    Delete its block, then apply

    When a resource block disappears from the configuration, the next apply plans its destruction, and leaves the rest alone. “Run terraform destroy in the directory” would destroy every resource that the configuration manages. “Delete the server in the cloud console” would leave the server in the configuration and in the state, so the next apply would create it again. And “Run terraform plan with -destroy” only previews the destruction of everything, and changes nothing.