Pin cluster configurations using the API

Pin up to 100 compute cluster configurations using the API.

Written by simran.arora

Last published at: December 21st, 2022

Normally, cluster configurations are automatically deleted 30 days after the cluster was last terminated.

If you want to keep specific cluster configurations, you can pin them. Up to 100 clusters can be pinned.

Pinned clusters are not automatically deleted, however they can be manually deleted.

Delete

Info

You must be a Databricks administrator to pin a cluster.

You can easily pin a cluster (AWS | Azure | GCP) via the workspace UI, but if you are managing your clusters via the API, you can also use the Pin endpoint (AWS | Azure | GCP) in the Clusters API.

Instructions

Pin all unpinned clusters

Delete

Info

A maximum of 100 clusters can be pinned. If you attempt to "pin all" via the API, the clusters are pinned based on the order in which they are listed in the response. Once a total of 100 clusters are pinned, no more can be pinned until some are removed.

Use the following sample code to pin all unpinned clusters in your workspace.

Before running the sample code, you will need a personal access token (AWS | Azure | GCP) and your workspace domain. The workspace domain is just the domain name.

  1. Copy and paste the sample code into a notebook cell.
  2. Update the <workspace-domain-without-trailing-backlash> and <personal-access-token> values.
  3. Run the cell to pin all unpinned clusters in your workspace.
%python

import requests
workspace_url = "<workspace-domain-without-trailing-backlash>"
access_token = "<personal-access-token>"

url = workspace_url + "/api/2.0/clusters/list"

headers = {
 'Authorization': 'Bearer ' + access_token
}

cluster = requests.request("GET", url, headers=headers).json()
for unpinned in cluster["clusters"]:
    if not 'pinned_by_user_name' in unpinned :
        print("Pinning"+" , "+ unpinned["default_tags"]['ClusterName'])
        url = workspace_url + "/api/2.0/clusters/pin"
        requests.post(url,json={"cluster_id" : unpinned["cluster_id"]},headers=headers)

Pin a cluster by name

Use the following sample code to pin a specific cluster in your workspace.

Before running the sample code, you will need a personal access token and your workspace domain. The workspace domain is just the domain name.

  1. Copy and paste the sample code into a notebook cell.
  2. Update the <workspace-domain-without-trailing-backlash> and <personal-access-token> values.
  3. Update the <cluster-name-to-pin> value with the name of the cluster you want to pin.
  4. Run the cell to pin the selected cluster in your workspace.
%python

import requests
workspace_url = "<workspace-domain-without-trailing-backlash>"
access_token = "<personal-access-token>"

url = workspace_url + "/api/2.0/clusters/list"

headers = {
 'Authorization': 'Bearer ' + access_token
}

cluster = requests.request("GET", url, headers=headers).json()

for unpinned in cluster["clusters"]:
    if not 'pinned_by_user_name' in unpinned :
        if unpinned["default_tags"]['ClusterName'] == "<cluster-name-to-pin>" :
            print("Pinning"+" , "+ unpinned["default_tags"]['ClusterName'])
            url = workspace_url + "/api/2.0/clusters/pin"
            requests.post(url,json={"cluster_id" : unpinned["cluster_id"]},headers=headers)

Pin all clusters by a specific user

Use the following sample code to pin a specific cluster in your workspace.

Before running the sample code, you will need a personal access token and your workspace domain. The workspace domain is just the domain name.

  1. Copy and paste the sample code into a notebook cell.
  2. Update the <workspace-domain-without-trailing-backalsh> and <personal-access-token> values.
  3. Update the <cluster-creator-username> value with the name of the user whose clusters you want to pin.
  4. Run the cell to pin the selected clusters in your workspace.
%python

import requests
workspace_url = "<workspace-domain-without-trailing-backlash>"
access_token = "<personal-access-token>"  

url = workspace_url + "/api/2.0/clusters/list"

headers={
 'Authorization': 'Bearer ' + access_token
}

cluster = requests.request("GET", url, headers=headers).json()

for unpinned in cluster["clusters"]:
     if not 'pinned_by_user_name' in unpinned :
        if unpinned["creator_user_name"] == "<cluster-creator-username>" :
            url = shard_url + "/api/2.0/clusters/pin"
            requests.post(url,json={"cluster_id" : unpinned["cluster_id"]},headers=headers)
            print("Pinning"+" , "+ unpinned["default_tags"]['ClusterName'])


Was this article helpful?