azure-cli: Deleting VM with 'az vm delete' does not delete its disk

If I delete a VM with az vm delete I expect all dependent resources to be deleted too. But disks are not.

E.g. I have VM tw-001-westeurope

kristian@opossum:~$ az vm show -n tw-001-westeurope -g tw-rg-westeurope
{
  "availabilitySet": null,
  "diagnosticsProfile": null,
  "hardwareProfile": {
    "vmSize": "Basic_A1"
  },
  "id": "/subscriptions/***/resourceGroups/tw-rg-westeurope/providers/Microsoft.Compute/virtualMachines/tw-001-westeurope",
  "identity": null,
  "instanceView": null,
  "licenseType": null,
  "location": "westeurope",
  "name": "tw-001-westeurope",
  "networkProfile": {
    "networkInterfaces": [
      {
        "id": "/subscriptions/***/resourceGroups/tw-rg-westeurope/providers/Microsoft.Network/networkInterfaces/tw-001-westeuropeVMNic",
        "primary": null,
        "resourceGroup": "tw-rg-westeurope"
      }
    ]
  },
  "osProfile": {
    "adminPassword": null,
    "adminUsername": "azureuser",
    "computerName": "tw-001-westeurope",
    "customData": null,
    "linuxConfiguration": {
      "disablePasswordAuthentication": true,
      "ssh": {
        "publicKeys": [
          {
            "keyData": "***",
            "path": "/home/azureuser/.ssh/authorized_keys"
          }
        ]
      }
    },
    "secrets": [],
    "windowsConfiguration": null
  },
  "plan": null,
  "provisioningState": "Succeeded",
  "resourceGroup": "tw-rg-westeurope",
  "resources": null,
  "storageProfile": {
    "dataDisks": [],
    "imageReference": {
      "id": "/subscriptions/***/resourceGroups/tw-rg-westeurope/providers/Microsoft.Compute/images/tw-image-2017-11-13",
      "offer": null,
      "publisher": null,
      "resourceGroup": "tw-rg-westeurope",
      "sku": null,
      "version": null
    },
    "osDisk": {
      "caching": "None",
      "createOption": "FromImage",
      "diskSizeGb": 30,
      "encryptionSettings": null,
      "image": null,
      "managedDisk": {
        "id": "/subscriptions/***/resourceGroups/tw-rg-westeurope/providers/Microsoft.Compute/disks/tw-001-westeurope",
        "resourceGroup": "tw-rg-westeurope",
        "storageAccountType": "Standard_LRS"
      },
      "name": "tw-001-westeurope",
      "osType": "Linux",
      "vhd": null
    }
  },
  "tags": {},
  "type": "Microsoft.Compute/virtualMachines",
  "vmId": "abdaa320-2fef-4c22-aca1-339682f21bfd",
  "zones": null
}

and delete it with az vm delete:

kristian@opossum:~$ az vm delete -g tw-rg-westeurope -n tw-001-westeurope --yes
{
  "endTime": "2017-11-16T13:48:11.344229+00:00",
  "error": null,
  "name": "6a7b12f7-779d-4014-9794-720feb670f0f",
  "startTime": "2017-11-16T13:47:09.907105+00:00",
  "status": "Succeeded"
}

the disk that was attached to the VM is still there:

kristian@opossum:~$ az disk show -n tw-001-westeurope -g tw-rg-westeurope
{
  "creationData": {
    "createOption": "Copy",
    "imageReference": null,
    "sourceResourceId": "/subscriptions/***/resourceGroups/tw-rg-westeurope/providers/Microsoft.Compute/snapshots/tw-image-2017-11-13_envfb1jz4qg",
    "sourceUri": null,
    "storageAccountId": null
  },
  "diskSizeGb": 30,
  "encryptionSettings": null,
  "id": "/subscriptions/***/resourceGroups/tw-rg-westeurope/providers/Microsoft.Compute/disks/tw-001-westeurope",
  "location": "westeurope",
  "managedBy": null,
  "name": "tw-001-westeurope",
  "osType": "Linux",
  "provisioningState": "Succeeded",
  "resourceGroup": "tw-rg-westeurope",
  "sku": {
    "name": "Standard_LRS",
    "tier": "Standard"
  },
  "tags": {},
  "timeCreated": "2017-11-16T12:28:51.081460+00:00",
  "type": "Microsoft.Compute/disks",
  "zones": null
}

Environment summary

Install Method (e.g. pip, interactive script, apt-get, Docker, MSI, edge build) / CLI version (az --version) / OS version / Shell Type (e.g. bash, cmd.exe, Bash on Windows)

azure-cli (2.0.20)

acr (2.0.14)
acs (2.0.18)
appservice (0.1.19)
backup (1.0.2)
batch (3.1.6)
batchai (0.1.2)
billing (0.1.6)
cdn (0.0.10)
cloud (2.0.9)
cognitiveservices (0.1.9)
command-modules-nspkg (2.0.1)
component (2.0.8)
configure (2.0.12)
consumption (0.1.6)
container (0.1.12)
core (2.0.20)
cosmosdb (0.1.14)
dla (0.0.13)
dls (0.0.16)
eventgrid (0.1.5)
extension (0.0.5)
feedback (2.0.6)
find (0.2.7)
interactive (0.3.11)
iot (0.1.13)
keyvault (2.0.13)
lab (0.0.12)
monitor (0.0.11)
network (2.0.17)
nspkg (3.0.1)
profile (2.0.15)
rdbms (0.0.8)
redis (0.2.10)
resource (2.0.17)
role (2.0.14)
servicefabric (0.0.5)
sql (2.0.14)
storage (2.0.18)
vm (2.0.17)

Python location '/opt/az/bin/python3'
Extensions directory '/home/kristian/.azure/cliextensions'

Python (Linux) 3.6.1 (default, Oct 18 2017, 20:41:18) 
[GCC 4.8.4]

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 8
  • Comments: 28 (15 by maintainers)

Most upvoted comments

I am no longer on the CLI team. Hmm, the CLI is a client of ARM / Compute, I think this would need to be a feature request to the Azure Compute Service.

cc: @qwordy @yonzhan

Do you think you could follow up on this issue?

Sure. I’ll look into it after holiday (1st Oct ~ 7th Oct).

As a new user of the az CLI, the number of disks I just found after iterating on a vm create; test; vm delete cycle was extremely surprising! Please implement something to help in this space! 😃

No, there currently isn’t a --tag filter on resource delete. I’m saying we should add it.

@tjprescott @yugangw-msft. We can possibly document the tags approach somewhere to let users know this is a viable option. Perhaps here.

But given the customer demand and that CLI 1.0 did expose an option for deleting disks. I think it won’t hurt to add such options as a convenience.

When you create a VM using --tags, all tags are propagated to any other created resources. So, one could clean up these resources by querying all resources with that VM’s tag and deleting them.

Instead of adding special-case arguments to vm delete, I would recommend adding client-side filters to the resource delete command.

3. Disk and Public IP are the 2 main things should be cleaned up

I think one could reasonably add the NIC and automatically created NSG (the one created if no --nsg param is given when creating the VM) to that list.

Created #8532 to provide a work around to this issue.

Though a better approach might be a delete_command template that adds filters to all delete commands, similar to wait_command and show_command. However, if I ran az vm delete --tag foo=bar I would expect ONLY VMs to be deleted, not other resources.

Deleting just the disk would not address the customer asks:

I expect all dependent resources to be deleted too.

It would be nice to have an option to clean up all the resources the VM is using when the VM is deleted

100%. This is a huge issue for any kind of automated process when you have to manually go in and clean up after the VMs all the time.

Deleting all resources based on a tag would accomplish this (unless it was impossible). Additionally, it would provide generally useful utility instead of narrow, limited utility.