MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / main

Function main

compute/deleteInstance.js:22–61  ·  view source on GitHub ↗

* Sends a delete request to GCP and waits for it to complete. * * @param {string} projectId - ID or number of the project you want to use. * @param {string} zone - Name of the zone you want to check, for example: us-west3-b * @param {string} instanceName - Name of the instance you want to delete

(projectId, zone, instanceName)

Source from the content-addressed store, hash-verified

20 * @param {string} instanceName - Name of the instance you want to delete.
21 */
22function main(projectId, zone, instanceName) {
23 // [START compute_instances_delete]
24 /**
25 * TODO(developer): Uncomment these variables before running the sample.
26 */
27 // const projectId = 'YOUR_PROJECT_ID';
28 // const zone = 'europe-central2-b'
29 // const instanceName = 'YOUR_INSTANCE_NAME';
30
31 const compute = require('@google-cloud/compute');
32
33 // Delete the instance specified by `instanceName` if it's present in the given project and zone.
34 async function deleteInstance() {
35 const instancesClient = new compute.InstancesClient();
36
37 console.log(`Deleting ${instanceName} from ${zone}...`);
38
39 const [response] = await instancesClient.delete({
40 project: projectId,
41 zone,
42 instance: instanceName,
43 });
44 let operation = response.latestResponse;
45 const operationsClient = new compute.ZoneOperationsClient();
46
47 // Wait for the delete operation to complete.
48 while (operation.status !== 'DONE') {
49 [operation] = await operationsClient.wait({
50 operation: operation.name,
51 project: projectId,
52 zone: operation.zone.split('/').pop(),
53 });
54 }
55
56 console.log('Instance deleted.');
57 }
58
59 deleteInstance();
60 // [END compute_instances_delete]
61}
62
63main(...process.argv.slice(2));

Callers 1

deleteInstance.jsFile · 0.70

Calls 1

deleteInstanceFunction · 0.70

Tested by

no test coverage detected