cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Terminating cluster programmatically

MichaelO
New Contributor III

Is there any python script that allows me to terminate (not delete)  a cluster in the notebook, similar to this R equivalent of

terminate_cluster(cluster_id, workspace, token = NULL, verbose = T, ...)
1 ACCEPTED SOLUTION

Accepted Solutions

Kaniz_Fatma
Community Manager
Community Manager

Hi @MichaelO, Yes, you can use the Databricks Python API for that. Here is an example Python function that terminates a cluster given a cluster id:

import requests
import json
import time

def terminate_cluster(cluster_id):
    # Define the endpoint for terminating a cluster
    endpoint = f"/api/2.0/clusters/delete"
    
    # Define the URL for the Databricks API
    domain = 'https://your_azure_databricks_instance/api'
    token = 'your_access_token'

    headers = {'Content-Type': 'application/json;charset=UTF-8',
                'Authorization': f'Bearer {token}'}
    
    # Define the JSON payload for terminating the cluster
    payload = {
        "cluster_id": cluster_id,
        "terminate": True
    }

    # Send the request to the Databricks API
    response = requests.post(domain + endpoint, headers=headers, data=json.dumps(payload))

    # Check if the request was successful
    if response.status_code == 200:
        print(f"Cluster {cluster_id} terminated successfully.")
    else:
        print(f"Failed to terminate Cluster {cluster_id}.")
        print(response.content)

You can call this function by passing the cluster_id as a parameter like this:

terminate_cluster("my_cluster_id")

Note: The token the parameter should be replaced by your Databricks personal access token and the domain parameter should be replaced by your domain name.

 

View solution in original post

1 REPLY 1

Kaniz_Fatma
Community Manager
Community Manager

Hi @MichaelO, Yes, you can use the Databricks Python API for that. Here is an example Python function that terminates a cluster given a cluster id:

import requests
import json
import time

def terminate_cluster(cluster_id):
    # Define the endpoint for terminating a cluster
    endpoint = f"/api/2.0/clusters/delete"
    
    # Define the URL for the Databricks API
    domain = 'https://your_azure_databricks_instance/api'
    token = 'your_access_token'

    headers = {'Content-Type': 'application/json;charset=UTF-8',
                'Authorization': f'Bearer {token}'}
    
    # Define the JSON payload for terminating the cluster
    payload = {
        "cluster_id": cluster_id,
        "terminate": True
    }

    # Send the request to the Databricks API
    response = requests.post(domain + endpoint, headers=headers, data=json.dumps(payload))

    # Check if the request was successful
    if response.status_code == 200:
        print(f"Cluster {cluster_id} terminated successfully.")
    else:
        print(f"Failed to terminate Cluster {cluster_id}.")
        print(response.content)

You can call this function by passing the cluster_id as a parameter like this:

terminate_cluster("my_cluster_id")

Note: The token the parameter should be replaced by your Databricks personal access token and the domain parameter should be replaced by your domain name.

 

Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!