← All posts

Pre-Writing Your Teardown Script: The Discipline That Saves Your Bill

The most expensive habit in personal Azure labs isn't picking the wrong SKU. It's forgetting to clean up. One simple discipline fixes it.

The pattern that's saved me hundreds of dollars in lab spend isn't a fancier cost dashboard. It's a rule:

Write the teardown script before the create script.

If you can't articulate how to delete the thing, you don't get to create it. This sounds austere. It's actually liberating — because you stop half-trusting yourself to "remember to clean up tomorrow."

Three rules

  1. Every lab gets its own resource group. rg-lab-... prefix. Never mix lab resources with permanent ones.
  2. Every lab has a scripts/lab-teardown.ps1 that takes the RG names as a parameter and supports -WhatIf.
  3. Every lab session ends with running the teardown script before you close the laptop.

Skeleton

[CmdletBinding(SupportsShouldProcess=$true)]
param(
    [string[]]$LabResourceGroups = @("rg-lab-dev-swc-001")
)

foreach ($rg in $LabResourceGroups) {
    if ($PSCmdlet.ShouldProcess($rg, "Delete RG and all contents")) {
        # Purge soft-deleted Key Vaults so they don't block recreation
        $kvs = az keyvault list -g $rg --query "[].name" -o tsv 2>$null
        foreach ($k in $kvs) {
            az keyvault delete -n $k -g $rg
            az keyvault purge -n $k 2>$null
        }
        az group delete --name $rg --yes --no-wait
    }
}

The Sunday checklist

Tape this to the side of the monitor:

  • [ ] ./lab-teardown.ps1 -WhatIf (read every line)
  • [ ] ./lab-teardown.ps1 (run for real)
  • [ ] Wait 10 minutes
  • [ ] az group list --query "[?contains(name,'lab')].name" returns empty
  • [ ] Cost dashboard back to baseline

The discipline is its own reward. The next morning's bill confirms it.

Chat with my AI