Viewing and applying idle PD recommendations

Compute Engine provides recommendations to help you identify persistent disks (PDs) that haven't been attached to any virtual machine (VM) instance for at least 15 days. You can use idle persistent disk recommendations to help minimize waste of resources and reduce your compute bill. For PDs that are not actively used, create a backup snapshot and delete the PD.

The Recommender generates one of the following recommended actions for idle disks:

  • Delete: When a disk is blank and has never been attached to any VM instance. You can delete the disk to save 100% of the cost of that disk (see disk pricing).
  • Snapshot: When data is stored on a disk that is idle. You can create a snapshot to store the data for later, and delete the disk to reduce the maintenance cost of that disk by 35% to 92% (see disk pricing).

After receiving a recommendation, you can take action to snapshot or delete the idle disk.

Before you begin

Pricing

Idle PD recommendations are available free of charge.

Recommendation generation

If a disk has not been attached to a VM for 15 days, the Recommender classifies that disk as idle.

One of the following conditions must be met in order to classify a PD as idle and generate the corresponding recommended action:

Condition for PD to be classified as idle Recommended action
Created at least 15 days ago AND
Never attached to a VM AND
Is blank
Delete
Detached for at least 15 days Create snapshot and delete

Recommendation frequency

Idle PD recommendations begin 15 days after PD creation, and they are updated once every 24 hours.

Viewing recommendations

To view recommendations about idle PDs, use the gcloud tool or the API.

gcloud

Use the gcloud recommender recommendations list command with the --recommender=google.compute.disk.IdleResourceRecommender flag:

gcloud recommender recommendations list \
    --project=project-id \
    --location=location \
    --recommender=google.compute.disk.IdleResourceRecommender \
    --format=yaml

Replace the following:

  • project-id: the ID of your project.
  • location: the zone or region for which to list recommendations, for example us-central1-c.

For example, the following command lists idle PD recommendations in us-central1-c for my-project:

gcloud recommender recommendations list \
    --project=test-project \
    --location=us-central1-c \
    --recommender=google.compute.disk.IdleResourceRecommender \
    --format=yaml


---
content:
    operationGroups:
    - operations:
      - action: add
        resource: //compute.googleapis.com/projects/test-project/global/snapshots/$snapshot-name
        resourceType: compute.googleapis.com/Disk
        path: /
        value:
        - name: $snapshot-name
          sourceDisk: projects/test-project/locations/us-central1-c/disks/pd-name
          storageLocations: us-central1
      - action: remove
        resource: //compute.googleapis.com/projects/test-project/zones/us-central1-c/disks/pd-name
        resourceType: compute.googleapis.com/Disk
        path: /
description: Save cost by snapshotting and then deleting idle persistent disk 'pd-name'
name: projects/test-project/locations/us-central1-c/recommenders/google.compute.disk.IdleResourceRecommender/recommendations/0fd31b24-cc05-4132-8431-ed54a22dd4f1
recommenderSubtype: SNAPSHOT_AND_DELETE_DISK
lastRefreshTime:
  seconds: 1543912652
primaryImpact:
  category: COST
  costProjection:
    cost:
      currencyCode: USD
      units: '-50'
    duration:
      seconds: 2592000
stateInfo: ACTIVE
etag: "cb0e6ac2cfc0b591"

Learn more about using gcloud tool to work with recommendations.

API

Call the recommendations.list method and specify the following recommender ID, google.compute.disk.IdleResourceRecommender:

GET https://recommender.googleapis.com/v1/projects/project-id/locations/location/recommenders/google.compute.disk.IdleResourceRecommender/recommendations

Replace the following:

  • project-id: the ID of your project.
  • location: the zone or region for which to list recommendations, for example us-central1-c.

If you aren't already familiar with the authentication prerequisites for calling Google Cloud APIs, see the Authentication overview.

The following example bash script uses the end user authentication flow with an OAuth client credential. The script uses the Google oauth2l command-line tool to get an OAuth 2.0 access token and then makes a curl request using the token.

PROJECT_ID=test-project
LOCATION=us-central1-c
RECOMMENDER=google.compute.disk.IdleResourceRecommender
OAUTH_JSON=~/service-account.json
OAUTH_HEADER="$(oauth2l header --json $OAUTH_JSON cloud-platform)"

curl -H "$OAUTH_HEADER" https://recommender.googleapis.com/v1/projects/$PROJECT_ID/locations/$LOCATION/recommenders/google.compute.disk.IdleResourceRecommender/recommendations

Example JSON response for idle PD recommendation:

{
 "description" : "Save cost by deleting idle persistent disk 'pd-name'",
 "name": "projects/test-project/locations/us-central1-c/recommenders/"
         "google.compute.disk.IdleResourceRecommender/"
         "recommendations/0fd31b24-cc05-4132-8431-ed54a22dd4f1",
 "recommenderSubtype" : "SNAPSHOT_AND_DELETE_DISK",
 "lastRefreshTime": {
   "seconds": 1543912652
 },
 "primaryImpact": {
   "category": COST,
   "costProjection": {
    "cost": {"currencyCode": "USD", "units": -50},
    "duration": { "seconds": 2592000 }
   }
 },
 "stateInfo": ACTIVE,
 "content": {
   "operationGroups" : [
    {
      "operations" : [
       {
         "action": "add",
         "resourceType": "compute.googleapis.com/Disk",
         "resource" : "//compute.googleapis.com/projects/test-project/global/snapshots/$snapshot-name",
         "value": {
           "name": "$snapshot-name",
           "sourceDisk": "projects/test-project/zones/us-central1-c/disks/pd-name",
           "storageLocations": ["us-central1"],
         }
       },
       {
         "action": "remove",
         "resourceType": "compute.googleapis/Disk",
         "resource": "//compute.googleapis.com/projects/test-project/"
                     "zones/us-central1-c/disks/pd-name"
       }
      ]
    }
   ]
 },
 "associatedInsights": [
    {
      "insight": "projects/test-project/locations/us-central1-c/insightTypes/google.compute.disk.IdleResourceInsight/insights/31326443-bcc3-4776-9b86-48879fddb656"
    }
  ],
 "etag": "cb0e6ac2cfc0b591"
}

Interpreting the recommendation response

Each recommendation that you receive through the gcloud tool or API contains an operations group, with operations that you can perform in serial to apply the recommendation. Idle PD recommendation operation groups include one or two operations:

  • If your idle PD has data on it, the recommendation includes an add operation to create a snapshot to back up the PD. For example:

       {
         "action": "add",
         "resourceType": "compute.googleapis.com/Disk",
         "resource" : "//compute.googleapis.com/projects/test-project/global/snapshots/$snapshot-name",
         "path": "/",
         "value": {
           "name": "$snapshot-name",
           "sourceDisk": "projects/test-project/zones/us-central1-c/disks/pd-name",
           "storageLocations": ["us-central1"]
         }
       }
    
  • A remove operation to permanently delete the idle PD. For example:

       {
         "action": "remove",
         "resourceType": "compute.googleapis.com/Disk",
         "resource" : "//compute.googleapis.com/projects/test-project/zones/us-central1-c/disks/pd-name",
         "path": "/"
       }
    

See Applying idle PD recommendations for example commands to create a snapshot of and to delete a PD.

Viewing insights for idle PD recommendations

The Recommender generates recommendations based on PD resource insights. By viewing insights on PD resources you can learn how long a PD has been detached from a VM and whether a PD is blank.

If you want to view the insight that generated the recommendation, you can use the gcloud tool or API.

gcloud

Use the insights list command

gcloud beta recommender insights list --project=project-name \
  --location=us-central1-c --insight-type=google.compute.disk.IdleResourceInsight

API

Use the insights.list method with google.compute.disk.IdleResourceInsights as the insight type ID.

GET https://recommender.googleapis.com/v1beta1/projects/project-id/locations/location/insightTypes/google.compute.disk.IdleResourceInsights/insights

Replace the following:

  • project-id: the ID of your project.
  • location: the zone or region that contains PDs to list insights for, for example us-central1-c.

Here is an example response of an insight on an idle PD resource:

{
  "name": "projects/test-project/locations/us-central1-c/insightTypes/google.compute.disk.IdleResourceInsight/insights/0ec21a13-bb04-3121-7321-dc43a11cc3e2",
  "description": "Disk 'pd-name' in zone 'us-central1-c' was last used 17 days ago. Consider taking snapshot and delete it.",
  "targetResources": [
    "//compute.googleapis.com/projects/test-project/zones/us-central1-c/disks/pd-name"
  ],
  "insightSubtype": "IDLE_DISK",
  "content": {
    "diskLastUseTime": "2019-10-01 13:00:00",
    "isBlank": false,
  },
  "lastRefreshTime": "2019-10-10 13:00:00",
  "observationPeriod": "15 days",
  "stateInfo": {
    "state": "ACTIVE"
  },
  "category": "COST",
  "etag": "fds421j2349",
  "associatedRecommendations": [
    {
      "projects/test-project/locations/us-central1-c/recommenders/"
      "google.compute.disk.IdleResourceRecommender/"
      "recommendations/0fd31b24-cc05-4132-8431-ed54a22dd4f1"
    }
  ]
}

The response includes the following fields:

  • diskLastUseTime: The last time when the disk was attached to a VM. If the disk was never attached to a VM this is set to the disk's creation time.
  • isBlank: This field is set to true if disk is blank and was never attached to VM. Otherwise it is false.

For more information about insights, see the reference docs.

Applying idle PD recommendations

If you receive a recommendation about an idle PD and you decide that you want to follow the recommendation, follow the instructions below to learn how to take a snapshot of a PD to back it up, delete the idle PD, and optionally recover the PD from the snapshot.

Taking a snapshot of an idle PD

gcloud

To create a snapshot of a zonal persistent disk in the default storage location, use the gcloud compute disks snapshot command:

gcloud compute disks snapshot disk-name

Replace the following:

  • disk-name: the name of the zonal persistent disk for which you want to create a snapshot.

API

To create a snapshot of a zonal persistent disk in the default storage location, make a POST request to the disks.createSnapshot method:

POST https://compute.googleapis.com/compute/v1/projects/project-id/zones/zone/disks/disk-name/createSnapshot

Replace the following:

  • project-id. Your project ID.
  • zone. The zone where your instance and disk are located.
  • disk-name. The name of the zonal persistent disk for which you want to create a snapshot.

For a guide on how to create a snapshot of a regional PD or in a custom storage location, see Creating persistent disk snapshots.

Deleting an idle PD

gcloud

To delete an idle disk, use the gcloud compute disks delete command:

gcloud compute disks delete disk-name \
[--zone=zone | --region=region]

Replace the following:

  • disk-name: the name of the zonal persistent disk that you want to delete.
  • zone: the zone where your disk is located.
  • region: the region where your disk is located.

API

To delete an idle disk, make a DELETE request to the disks.delete method:

DELETE https://compute.googleapis.com/compute/v1/projects/project-id/zones/zone/disks/disk-name

Replace the following:

  • project-id. Your project ID.
  • zone. The zone where your disk is located.
  • disk-name. The name of the persistent disk that you want to delete.

For a regional PD, replace zones/zone with regions/region.

Recovering PD from a snapshot

For information about how to recover data from a snapshot and recreate your PD, see Restoring persistent disk snapshots.