Skip to content

Instantly share code, notes, and snippets.

@kamarton
Created March 15, 2025 15:57
Show Gist options
  • Save kamarton/96f7889d0e8b0e88d2861e8fbbde22a6 to your computer and use it in GitHub Desktop.
Save kamarton/96f7889d0e8b0e88d2861e8fbbde22a6 to your computer and use it in GitHub Desktop.
Stop compute engine, detach disk, (delete snapshot id exists), create snapshot, delete disk (GCP, CLI, GCLOUD)
#!/usr/bin/env bash
PROJECT=<project>
ZONE=<zone>
INSTANCE=<compute-engine-instance>
DISK=<disk-name>
SNAPSHOT=<snapshot-name>
REGION=<region>
echo stop vm - - - - - - - - - - - - - - - - - - - - - -
gcloud compute instances stop $INSTANCE \
--project=$PROJECT
echo detach disk - - - - - - - - - - - - - - - - - - - -
gcloud compute instances detach-disk $INSTANCE \
--disk $DISK \
--project=$PROJECT \
--zone $ZONE
echo delete snapshot - - - - - - - - - - - - - - - - - -
gcloud compute snapshots delete $SNAPSHOT \
--project=$PROJECT \
--zone $ZONE
echo create snapshot from disk - - - - - - - - - - - - - - - -
gcloud compute snapshots create $SNAPSHOT \
--project=$PROJECT \
--source-disk=$DISK \
--source-disk-zone=$ZONE \
--storage-location=$REGION
echo delete disk - - - - - - - - - - - - - - - - - - - - - - - -
gcloud compute disks delete $DISK \
--zone $ZONE \
--project=$PROJECT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment