| 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] |