INVALID_STATE - Invalid State: Resource State Mismatch
INVALID_STATE means the resource is in a state that blocks your operation—trying to stop an already-stopped VM, delete a resource mid-creation, or modify a Cloud SQL database during backup. This can be client-side (wrong resource state) or server-side (resource locked by another operation). Most common in Compute Engine when operating on VMs in wrong states, but also appears in Cloud SQL during maintenance windows, GKE during cluster upgrades (RECONCILING state), and BigQuery when querying tables being deleted. GCP validates resource states before operations—state machines vary by service, and some transitions require intermediate steps.
#Common Causes
- →Resource State Mismatch: The resource is in a state that doesn't allow the operation. For example, stopping a VM that's already stopped, or deleting a resource that's being created. This can be persistent if the state won't change, or transient if the state will change.
- →Invalid State Transition: The requested state transition isn't allowed. For example, transitioning directly from TERMINATED to RUNNING without starting first. This is persistent—you must follow valid state transitions.
- →Resource Locked: The resource is locked by another operation. Locks prevent concurrent modifications. This is transient—waiting for the lock to release and retrying helps.
- →Service-Specific State Behavior: State behavior varies by service. Some services have more complex state machines than others (e.g., GKE has RECONCILING state, Cloud SQL has MAINTENANCE state).
✓Solutions
- 1Step 1: Diagnose - Check the resource's current state: gcloud compute instances describe INSTANCE_NAME --zone ZONE --format="get(status)" --project PROJECT_ID Or for Cloud SQL: gcloud sql instances describe INSTANCE_NAME --format="get(state)" --project PROJECT_ID
- 2Step 2: Diagnose - Check for in-progress operations that might be locking the resource: gcloud compute operations list --filter="status:RUNNING AND targetLink:*instances/INSTANCE_NAME" --project PROJECT_ID
- 3Step 3: Diagnose - Review API documentation for valid state transitions for your service.
- 4Step 4: Fix - Change resource state if needed. For example, start a stopped VM: gcloud compute instances start INSTANCE_NAME --zone ZONE --project PROJECT_ID
- 5Step 5: Fix - Wait for dependencies or in-progress operations to complete: gcloud compute operations wait OPERATION_ID --zone ZONE --project PROJECT_ID
- 6Step 6: Fix - Follow valid state transitions. Check API documentation and perform intermediate state changes if needed.
- 7Step 7: Verify - Retry the operation after state/dependencies are ready.
</>Code Examples
1# This script checks resource state and dependencies before operations to avoid INVALID_STATE errors
2
3PROJECT_ID="my-project"
4INSTANCE_NAME="my-instance"
5ZONE="us-central1-a"
6
7# Step 1: Check resource current state
8echo "Checking instance state..."
9STATE=$(gcloud compute instances describe $INSTANCE_NAME \
10 --zone $ZONE \
11 --project $PROJECT_ID \
12 --format="value(status)")
13
14echo "Current state: $STATE"
15
16# Step 2: Check for in-progress operations
17echo "Checking for in-progress operations..."
18OPERATIONS=$(gcloud compute operations list \
19 --filter="status:RUNNING AND targetLink:*instances/$INSTANCE_NAME" \
20 --format="value(name)" \
21 --project $PROJECT_ID)
22
23if [ ! -z "$OPERATIONS" ]; then
24 echo "Found in-progress operations. Waiting for them to complete..."
25 for OP in $OPERATIONS; do
26 echo "Waiting for operation $OP..."
27 gcloud compute operations wait $OP --zone $ZONE --project $PROJECT_ID
28 done
29fi
30
31# Step 3: If state doesn't allow operation, change it
32# Example: Start instance if it's stopped and we need it running
33if [ "$STATE" != "RUNNING" ]; then
34 echo "Instance is $STATE, starting..."
35 gcloud compute instances start $INSTANCE_NAME --zone $ZONE --project $PROJECT_ID
36 echo "Waiting for instance to be RUNNING..."
37 gcloud compute instances wait-until-running $INSTANCE_NAME --zone $ZONE --project $PROJECT_ID
38 echo "Instance is now RUNNING"
39fi
40
41# Step 4: Now perform your operation
42echo "Preconditions met. Proceeding with operation..."↗Related Errors
Provider Information
This error code is specific to GCP services. For more information, refer to the official GCP documentation.