terraform destroy
terraform destroy= ลบทุก resource ที่ Terraform manage — ระวังให้มาก!
Destroy ทำอะไร?
ลบ ทุก resource ที่อยู่ใน state — ตรงข้ามกับ apply
terraform destroy
ตัวอย่าง Output
Terraform will perform the following actions:
# aws_instance.web will be destroyed
- resource "aws_instance" "web" {
- id = "i-1234567890abcdef0" -> null
- instance_type = "t2.micro" -> null
...
}
# aws_s3_bucket.data will be destroyed
- resource "aws_s3_bucket" "data" {
- id = "my-data-bucket" -> null
- bucket = "my-data-bucket" -> null
...
}
Plan: 0 to add, 0 to change, 2 to destroy.
Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value: yes
aws_instance.web: Destroying... [id=i-1234567890abcdef0]
aws_instance.web: Destruction complete after 30s
aws_s3_bucket.data: Destroying... [id=my-data-bucket]
aws_s3_bucket.data: Destruction complete after 1s
Destroy complete! Resources: 2 destroyed.
Use Cases
1. ลบ Dev Environment ตอนกลางคืน
# Cron job: 22:00 ทุกวัน
terraform destroy -auto-approve -var-file=dev.tfvars
# Cron job: 08:00 ทุกวัน — สร้างใหม่
terraform apply -auto-approve -var-file=dev.tfvars
→ ประหยัดค่า cloud 70% (16 ชั่วโมง/วัน)
2. ลบ Feature Branch Environment
# PR closed → cleanup
terraform destroy -var-file="feature-${BRANCH}.tfvars"
3. ลบ Test Environment
# หลัง integration test
terraform destroy -auto-approve
Useful Flags
-auto-approve
terraform destroy -auto-approve
ใน CI/CD ใช้ flag นี้
-target
# ลบเฉพาะ resource เดียว
terraform destroy -target="aws_instance.web"
-var / -var-file
terraform destroy -var-file="dev.tfvars"
-parallelism
terraform destroy -parallelism=20
Destroy ผ่าน Plan Approach
แทนที่จะ terraform destroy ตรงๆ:
# 1. Plan destroy
terraform plan -destroy -out=tfplan
# 2. Review (สำคัญมากใน prod!)
terraform show tfplan
# 3. Apply destroy plan
terraform apply tfplan
→ Same effect, แต่มี review step
Targeted Destroy
ลบเฉพาะ resource:
terraform destroy -target="aws_instance.bastion"
terraform destroy -target="module.network"
ใช้ตอน:
- Migration — ลบ resource ตัวเดียวที่ไม่ได้ใช้แล้ว
- Debugging — ลบ + recreate resource ที่ broken
- Refactor — ย้าย resource จาก state นึงไปอีก state นึง
ถ้า aws_instance.web อ้าง aws_subnet.public — ลบ subnet ก่อน → instance อาจ orphan
ใช้ Terraform จัดการ dependency โดยลบ resource ที่ "พึ่งพิง" ก่อน
Resource ที่ Protected
prevent_destroy
resource "aws_db_instance" "prod" {
# ...
lifecycle {
prevent_destroy = true
}
}
$ terraform destroy
Error: Instance cannot be destroyed
Resource aws_db_instance.prod has lifecycle.prevent_destroy set to true.
ตอนอยากลบจริง:
- ลบ
prevent_destroy = trueออกจาก code terraform apply(ไม่ใช่ destroy — แค่ remove protection)terraform destroy
deletion_protection (provider-specific)
resource "aws_db_instance" "prod" {
deletion_protection = true # AWS-level protection
}
ต้องเปลี่ยนเป็น false ก่อน apply, แล้วถึง destroy ได้
S3 Bucket ที่มี Object
Error: BucketNotEmpty
แก้:
resource "aws_s3_bucket" "data" {
bucket = "my-data"
force_destroy = true # ลบ object ทุกตัวพร้อม bucket
}
- ❌ อย่าใช้ใน prod ที่มีข้อมูลจริง!
- ✅ ใช้ใน dev/test environment
Destroy แค่บางส่วน (ไม่ใช้ destroy)
แทน destroy → ใช้:
1. ลบ resource จาก config + apply
# ลบ block ออกจาก main.tf
# resource "aws_instance" "old" { ... } ← ลบทิ้ง
# Apply
terraform apply
# Plan: 0 to add, 0 to change, 1 to destroy.
→ ลบเฉพาะ resource ที่ไม่อยู่ใน config แล้ว
2. ลบเฉพาะ state (ไม่ลบ cloud)
terraform state rm "aws_instance.web"
→ Terraform เลิก manage resource นี้ — แต่ resource ยังอยู่ใน cloud
ใช้ตอน:
- ย้าย resource ไป state file อื่น
- import เข้า module อื่น
- Resource ถูกลบใน console แล้ว
Common Errors
CannotDelete (Resource has Dependencies)
Error: DependencyViolation
The vpc 'vpc-12345' has dependencies and cannot be deleted.
→ มี ENI / Lambda / etc. ที่ AWS provider ไม่รู้ว่าต้องลบก่อน
แก้: ลบ resource เหล่านั้นใน console หรือเพิ่มใน Terraform แล้ว destroy ใหม่
Subnet has running instances
Error: DependencyViolation: subnet has dependencies
แก้: ลบ instance ก่อน หรือ Terraform จะลบ instance อัตโนมัติถ้า manage ทั้งคู่
State Lock
Error: Error acquiring the state lock
แก้: รอคนอื่นเสร็จ หรือ terraform force-unlock <ID>
Destroy Strategies
Strategy 1: All-or-Nothing
terraform destroy
ลบทุกอย่างใน state
Strategy 2: Layer by Layer
ถ้าใช้ multiple state files:
infra/
├── network/ # destroy ทีหลัง
├── database/ # destroy กลาง
└── application/ # destroy ก่อน
cd application && terraform destroy
cd ../database && terraform destroy
cd ../network && terraform destroy
→ ดูที่ Splitting State Files
Strategy 3: Workspace
terraform workspace select dev
terraform destroy
terraform workspace select staging
terraform destroy
Destroy ใน CI/CD
on:
pull_request:
types: [closed]
jobs:
cleanup:
if: github.event.pull_request.head.ref != 'main'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: hashicorp/setup-terraform@v3
- run: terraform init
- name: Destroy ephemeral environment
run: terraform destroy -auto-approve -var="env=pr-${{ github.event.pull_request.number }}"
Best Practices
✅ DO:
- ใช้ prevent_destroy กับ prod database / S3 bucket ที่มีข้อมูล
- Plan ก่อน destroy ใน prod (-out=tfplan)
- ใช้ destroy สำหรับ dev/staging cleanup
- Backup data ก่อน destroy (snapshot)
❌ DON'T:
- ห้ามใช้ destroy ใน prod โดยไม่ review plan
- ห้ามใส่ -auto-approve ใน prod local terminal
- ห้ามใช้ force_destroy กับ S3 ที่มีข้อมูลจริง
- ห้ามใช้ destroy เมื่อแค่อยากลบ resource บางตัว — แก้ config + apply แทน
สรุป
terraform destroy= ลบทุก resource ใน state- ใช้
prevent_destroy = trueกับ resource สำคัญ -targetลบเฉพาะตัว,-auto-approveskip confirmation- Plan-based destroy:
plan -destroy -out=tfplan→apply tfplan - Multi-layer state — destroy ตามลำดับ (app → db → network)
ต่อไป → Section 10: State Management