简体   繁体   English

从Google Compute Engine删除所有停止的实例

[英]Delete all stopped instances from Google Compute Engine

I have some thousands of VM instances on Google Compute Engine. 我在Google Compute Engine上有数千个VM实例。 Almost all of them are stopped. 几乎所有它们都停止了。 How can I delete all the stopped instances at once? 如何一次删除所有已停止的实例? (doing so on the UI will take ages, and furthermore - the UI crashes) (在UI上这样做会花费一些时间,此外-UI崩溃)

Thanks! 谢谢!

First get the list of VMs from your project: 首先从您的项目中获取虚拟机列表:

gcloud compute instances list | grep TERMINATE

Verify that all these VMs need to be deleted. 验证是否需要删除所有这些VM。 Then following generates the commands you can execute to delete them all. 然后,以下命令将生成,您可以执行删除所有命令。 You can redirect the output to a file and then run "bash ". 您可以将输出重定向到文件,然后运行“ bash”。 Feel free to optimize to a single command line if you are feeling lucky :) 如果您感到幸运,可以随意优化为单个命令行:)

gcloud compute instances list | grep TERMINATE | awk '{printf "gcloud comoute instances delete %s --zone %s\n", $1, $2}' 
yes Y | gcloud compute instances list | awk '/TERMINATE/ {printf "gcloud compute instances delete %s --zone %s; ", $1, $2}' | bash

gcloud compute instances list : list instances one by one. gcloud计算实例列表:逐一列出实例。

awk: prints out "gcloud compute instances delete "terminated_instance_name" --zone "zone name that instance belongs to" awk:打印出“ gcloud计算实例,删除“ terminated_instance_name” --zone“实例所属的区域名称”

Pipe this output to bash to make it execute on terminal; 通过管道将此输出传输到bash以使其在终端上执行;

And "yes Y" supplies a 'Y' or yes answer when prompted for confirmation. 当提示您进行确认时,“是Y”会提供“是”或“是”答案。

terminated instances show in the gcloud console as stopped . 终止实例在gcloud控制台显示为停止 assuming you wish to delete terminated instances, you can look for instances which have a status of TERMINATED. 假设您希望删除终止的实例,则可以查找状态为TERMINATED的实例。

the other answers here will work but they will iterate through all of your instances. 此处的其他答案将起作用,但它们将遍历您的所有实例。 a slightly cleaner approach is to request a filtered instance list from gcloud so that you only iterate through instances which are already known to be in this state. 一种更简洁的方法是从gcloud请求经过过滤的实例列表,以便您仅迭代已知处于此状态的实例。

finally, i've found that secondary disks don't always delete when their parent instance deletes, so i also like to purge orphaned disks during cleanup. 最终,我发现辅助磁盘在其父实例删除时并不总是删除,因此我也想在清理过程中清除孤立的磁盘。

something like this should do it (for a bash shell): 像这样的事情应该做到(对于bash shell):

#!/bin/bash

# delete terminated instances
for terminated_instance_uri in $(gcloud compute instances list --uri --filter="status:TERMINATED" 2> /dev/null); do
  terminated_instance_name=${terminated_instance_uri##*/}
  terminated_instance_zone_uri=${terminated_instance_uri/\/instances\/${terminated_instance_name}/}
  terminated_instance_zone=${terminated_instance_zone_uri##*/}
  if [ -n "${terminated_instance_name}" ] && [ -n "${terminated_instance_zone}" ] && gcloud compute instances delete ${terminated_instance_name} --zone ${terminated_instance_zone} --delete-disks all --quiet; then
    echo "deleted: ${terminated_instance_zone}/${terminated_instance_name}"
  fi
done

# delete orphaned disks (filter for disks without a user)
for orphaned_disk_uri in $(gcloud compute disks list --uri --filter="-users:*" 2> /dev/null); do
  orphaned_disk_name=${orphaned_disk_uri##*/}
  orphaned_disk_zone_uri=${orphaned_disk_uri/\/disks\/${orphaned_disk_name}/}
  orphaned_disk_zone=${orphaned_disk_zone_uri##*/}
  if [ -n "${orphaned_disk_name}" ] && [ -n "${orphaned_disk_zone}" ] && gcloud compute disks delete ${orphaned_disk_name} --zone ${orphaned_disk_zone} --quiet; then
    echo "deleted: ${orphaned_disk_zone}/${orphaned_disk_name}"
  fi
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM