<p>I'm new to GKE-Python. I would like to delete my GKE(Google Kubernetes Engine) cluster using a python script.
I found an API <strong>delete_cluster()</strong> from the <strong>google-cloud-container</strong> python library to delete the GKE cluster.
<a href="https://googleapis.dev/python/container/latest/index.html" rel="nofollow noreferrer">https://googleapis.dev/python/container/latest/index.html</a></p>
<p>But I'm not sure how to use that API by passing the required parameters in python. Can anyone explain me with an example?</p>
<p>Or else If there is any other way to delete the GKE cluster in python?</p>
<p>Thanks in advance.</p>

<p>First you'd need to configure the Python Client for Google Kubernetes Engine as explained on <a href="https://googleapis.dev/python/container/latest/index.html#installation" rel="nofollow noreferrer">this section</a> of the link you shared. Basically, set up a <a href="https://docs.python.org/3.7/library/venv.html" rel="nofollow noreferrer">virtual environment</a> and install the library with <code>pip install google-cloud-container</code>.</p>
<p>If you are running the script within an environment such as the <a href="https://cloud.google.com/shell/docs" rel="nofollow noreferrer">Cloud Shell</a> with an user that has enough access to manage the GKE resources (with at least the <a href="https://cloud.google.com/kubernetes-engine/docs/how-to/iam#container.clusterAdmin" rel="nofollow noreferrer">Kubernetes Engine Cluster Admin</a> permission assigned) the client library will handle the necessary authentication from the script automatically and the following script will most likely work:</p>
<pre><code>from google.cloud import container_v1

project_id = &quot;YOUR-PROJECT-NAME&quot; #Change me. 
zone = &quot;ZONE-OF-THE-CLUSTER&quot; #Change me.
cluster_id = &quot;NAME-OF-THE-CLUSTER&quot; #Change me.
name = &quot;projects/&quot;+project_id+&quot;/locations/&quot;+zone+&quot;/clusters/&quot;+cluster_id

client = container_v1.ClusterManagerClient()

response = client.delete_cluster(name=name)

print(response)
</code></pre>
<p>Notice that as per the <a href="https://googleapis.dev/python/container/latest/gapic/v1/api.html#google.cloud.container_v1.ClusterManagerClient.delete_cluster" rel="nofollow noreferrer">delete_cluster</a> method documentation you only need to pass the <code>name</code> parameter. If by some reason you are just provided the credentials (generally in the form of a JSON file) of a service account that has enough permissions to delete the cluster you'd need to modify the client for the script and use the <a href="https://googleapis.dev/python/container/latest/gapic/v1/api.html#google.cloud.container_v1.ClusterManagerClient.delete_cluster" rel="nofollow noreferrer">credentials</a> parameter to get the client correctly authenticated in a similar fashion to:</p>
<pre><code>...
client = container_v1.ClusterManagerClient(credentials=credentials)
...
</code></pre>
<p>Where the <code>credentials</code> variable is pointing to the JSON filename (and path if it's not located in the folder where the script is running) of the service account credentials file with enough permissions that was provided.</p>
<p>Finally notice that the <code>response</code> variable that is returned by the <code>delete_cluster</code> method is of the <a href="https://googleapis.dev/python/container/latest/gapic/v1/types.html#google.cloud.container_v1.types.Operation" rel="nofollow noreferrer">Operations class</a> which can serve to monitor a long running operation in a similar fashion as to how it is explained <a href="https://cloud.google.com/dialogflow/docs/how/long-running-operations#get" rel="nofollow noreferrer">here</a> with the <code>self_link</code> attribute corresponding to the long running operation.</p>
<p>After running the script you could use a curl command in a similar fashion to:</p>
<pre><code>curl -X GET \
-H &quot;Authorization: Bearer &quot;$(gcloud auth application-default print-access-token) \
https://container.googleapis.com/v1/projects/[RPOJECT-NUMBER]/zones/[ZONE-WHERE-THE-CLUSTER-WAS-LOCATED]/operations/operation-[OPERATION-NUMBER]
</code></pre>
<p>by checking the <code>status</code> field (which could be in RUNNING state while it is happening) of the response to that curl command. Or your could also use the <a href="https://requests.readthedocs.io/en/master/" rel="nofollow noreferrer">requests</a> library or any equivalent to automate this checking procedure of the long running operation within your script.</p>
