0
votes

I am deleting VM from azure ARM. When I deleted VM using java-sdk, VM deleted successfully but disks and network are not deleted. It kept VHD files in storage. I tried to detach disk also but same it will detach disk from VM but not deleting VHDs.

Delete VM :-

azure.virtualMachines().delete("resourceGroupName", "vmName");

Detach disk:-

azure.virtualMachines().getByGroup("resourceGroupName", "vmName");
vm.update().withoutDataDisk("diskName").apply(); 

even after performing above operation VHD file exists in storage. How can we delete permanently all attached disk when I delete VM?.

2

2 Answers

0
votes

This is solved by Azure-Storage. You have to delete blob vhd files from attached storage account.

        CloudStorageAccount cloudStorageAccount = CloudStorageAccount.parse(storageConnectionString);
        CloudBlobClient cloudBlobClient = cloudStorageAccount.createCloudBlobClient();
        Iterable<CloudBlobContainer>  containersList= cloudBlobClient.listContainers();
        for(CloudBlobContainer container:containersList)
        {
            String currentContainerName = container.getName();
            logger.debug("Current Container Name : "+currentContainerName);
            CloudBlobContainer blobContainer = cloudBlobClient.getContainerReference(currentContainerName);
            CloudPageBlob pageBlob = blobContainer.getPageBlobReference(diskName);
            if(pageBlob.exists())  
                return pageBlob.deleteIfExists();
        }

More detail refer azure storage git

0
votes

@PawanSharma, As I known, if you want to delete VM and its related all resources once, you only need to delete the resource group which includes the VM and all other resources, or else manually delete these resources one by one.

According to your code, I judge out that you are using the version 1.0.0-beta of Azure SDK for Java, so I suggest that you can try to use the code below to do it.

 azure.resourceGroups().delete("<resource-group-name>");

As reference, you can refer to the sample code on GitHub.

Meanwhile, the other way without Azure SDK is that using Azure REST API Delete a resource group which I think it's more clear way for understanding.