Azure Resource Manager 資源群組和資源刪除Azure Resource Manager resource group and resource deletion

本文說明如何刪除資源群組和資源。This article shows how to delete resource groups and resources. 其中說明當您刪除資源群組時,Azure Resource Manager 如何排序資源刪除。It describes how Azure Resource Manager orders the deletion of resources when you delete a resource group.

如何判斷刪除的順序How order of deletion is determined

當您刪除資源群組時,Resource Manager 會決定刪除資源的順序。When you delete a resource group, Resource Manager determines the order to delete resources. 它使用下列順序:It uses the following order:

  1. 刪除所有子 (巢狀) 資源。All the child (nested) resources are deleted.

  2. 接著刪除管理其他資源的資源。Resources that manage other resources are deleted next. 資源可以設定 managedBy 屬性,指出由不同的資源來管理它。A resource can have the managedBy property set to indicate that a different resource manages it. 設定此屬性時,會先刪除管理其他資源的資源,再刪除這些資源。When this property is set, the resource that manages the other resource is deleted before the other resources.

  3. 在前兩個類別之後,刪除其餘資源。The remaining resources are deleted after the previous two categories.

決定順序之後,Resource Manager 會針對每個資源發出 DELETE 作業。After the order is determined, Resource Manager issues a DELETE operation for each resource. 它會等候任何相依性完成,再繼續進行。It waits for any dependencies to finish before proceeding.

針對同步作業,預期的成功回應碼為:For synchronous operations, the expected successful response codes are:

  • 200200
  • 204204
  • 404404

針對非同步作業,預期的成功回應碼為 202。For asynchronous operations, the expected successful response is 202. Resource Manager 會追蹤位置標頭和 azure-async 作業標頭,以判斷非同步刪除作業的狀態。Resource Manager tracks the location header or the azure-async operation header to determine the status of the asynchronous delete operation.

刪除錯誤Deletion errors

當刪除作業傳回錯誤時,Resource Manager 會重試 DELETE 呼叫。When a delete operation returns an error, Resource Manager retries the DELETE call. 若狀態碼為 5xx、429 和 408,則會發生重試。Retries happen for the 5xx, 429 and 408 status codes. 根據預設,重試的時間週期為 15 分鐘。By default, the time period for retry is 15 minutes.

刪除後After deletion

Resource Manager 會在嘗試刪除的每個資源上發出 GET 呼叫。Resource Manager issues a GET call on each resource that it tried to delete. 此 GET 呼叫的回應必須是 404。The response of this GET call is expected to be 404. 當 Resource Manager 取得 404 時,則刪除已成功完成。When Resource Manager gets a 404, it considers the deletion to have completed successfully. Resource Manager 會從其快取中移除資源。Resource Manager removes the resource from its cache.

不過,如果資源上的 GET 呼叫傳回 200 或 201,Resource Manager 會重新建立資源。However, if the GET call on the resource returns a 200 or 201, Resource Manager recreates the resource.

如果 GET 作業傳回錯誤,Resource Manager 會針對下列錯誤碼重試 GET:If the GET operation returns an error, Resource Manager retries the GET for the following error code:

  • 小於 100Less than 100
  • 408408
  • 429429
  • 大於 500Greater than 500

若是其他錯誤碼,則 Resource Manager 無法刪除資源。For other error codes, Resource Manager fails the deletion of the resource.

刪除資源群組Delete resource group

使用下列其中一種方法來刪除資源群組。Use one of the following methods to delete the resource group.

Remove-AzResourceGroup -Name ExampleResourceGroup

刪除資源Delete resource

使用下列其中一種方法來刪除資源。Use one of the following methods to delete a resource.

Remove-AzResource `
  -ResourceGroupName ExampleResourceGroup `
  -ResourceName ExampleVM `
  -ResourceType Microsoft.Compute/virtualMachines

後續步驟Next steps