TerraformPilot

Troubleshooting

Fix Terraform Error - GCP Cloud SQL Instance Already Exists

Fix Google Cloud SQL instance name conflicts in Terraform. Handle deletion cooldowns, name reuse restrictions, imports, and instance restoration.

LLuca Berton2 min read

Quick Answer

#

A Cloud SQL instance with the same name exists, or the name was recently deleted and is in a 7-day cooldown. GCP Cloud SQL instance names cannot be reused for up to a week after deletion. Use a different name with a timestamp or random suffix.

The Error

#
Error: Error creating DatabaseInstance: googleapi: Error 409:
  The Cloud SQL instance already exists. When you delete an instance,
  you can't reuse the name of the deleted instance until one week
  from the deletion date.

What Causes This Error

#

1. Instance Already Exists

#

A Cloud SQL instance with the same name is active in the project.

2. 7-Day Name Cooldown

#

GCP reserves deleted Cloud SQL instance names for 7 days. You cannot reuse the name during this period — there is no workaround.

3. Instance in Another Project

#

Cloud SQL names are unique per project, but the error might come from a deleted instance in the same project.

How to Fix It

#

Solution 1: Use Unique Names with Random Suffix

#
resource "random_id" "db" {
  byte_length = 4
}
 
resource "google_sql_database_instance" "main" {
  name             = "${var.project}-${var.environment}-db-${random_id.db.hex}"
  database_version = "POSTGRES_15"
  region           = var.region
 
  settings {
    tier = "db-custom-2-8192"
 
    ip_configuration {
      ipv4_enabled    = false
      private_network = google_compute_network.main.id
    }
 
    backup_configuration {
      enabled                        = true
      point_in_time_recovery_enabled = true
    }
  }
 
  deletion_protection = true
}

Solution 2: Import the Existing Instance

#
# Check if instance exists
gcloud sql instances describe my-db --project my-project
 
# Import into Terraform
# Format: projects/PROJECT/instances/INSTANCE_NAME
terraform import google_sql_database_instance.main \
  projects/my-project/instances/my-db

Solution 3: Wait for Cooldown (or Use a New Name)

#
# Check when the instance was deleted
gcloud sql instances list --project my-project --show-deleted
 
# If within 7 days, you MUST use a different name
# Append date or random suffix

Solution 4: Use deletion_protection

#
resource "google_sql_database_instance" "main" {
  name                = "${var.project}-${var.environment}-db-v2"
  deletion_protection = true  # Prevents accidental deletion
 
  settings {
    tier = "db-custom-2-8192"
  }
}

Cloud SQL Name Restrictions

#
RuleDetails
Globally unique per projectCan't have two instances with same name in one project
7-day cooldown after deletionName reserved for 1 week after delete
No workaround for cooldownMust wait or use a different name
Max length98 characters
CharactersLowercase letters, numbers, hyphens

Troubleshooting Checklist

#
  1. ✅ Does an active instance with this name exist? (gcloud sql instances describe)
  2. ✅ Was an instance with this name recently deleted? (7-day cooldown)
  3. ✅ Can you use a different name with a suffix?
  4. ✅ Should you import the existing instance?

Prevention Tips

#
  • Use random suffixes in Cloud SQL instance names — avoids cooldown issues entirely
  • Enable deletion_protection — prevents accidental deletion
  • Never reuse Cloud SQL names — always append a version or random ID
  • Document the 7-day rule in team runbooks — it surprises everyone the first time
  • Use prevent_destroy lifecycle rule for production databases
#

Conclusion

#

Cloud SQL instance names have a 7-day cooldown after deletion — no exceptions. Always use random suffixes in names, enable deletion_protection, and import existing instances instead of recreating them. This is the #1 gotcha that trips up GCP Terraform users.

#Terraform#Google Cloud#Troubleshooting#Error Fix

Share this article