1

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?.

Pawan Kumar
  • 1,511
  • 4
  • 15
  • 18
  • See [Removing VHD's from Azure Resource Manager after removing VM](http://stackoverflow.com/a/35210518/5221149). – Andreas Aug 30 '16 at 17:34

3 Answers3

0

@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.

Peter Pan
  • 23,476
  • 4
  • 25
  • 43
  • @peter-pan-msft This is working. I tried this it is working. As per my requirements not to delete resource group only delete disk. Is there anything to delete disk. I found delete blob from storage. Do you have any idea will it work or not. – Pawan Kumar Aug 31 '16 at 06:21
  • @PawanSharma Try to use the REST API [`Delete VM Image`](https://msdn.microsoft.com/en-us/library/azure/dn499769.aspx) or the function [`VirtualMachineVMImageOperations.deleteAsync(String vmImageName, boolean deleteFromStorage)`](http://azure.github.io/azure-sdk-for-java/com/microsoft/windowsazure/management/compute/VirtualMachineVMImageOperations.html#deleteAsync-java.lang.String-boolean-) of Azure Java SDK in ASM. – Peter Pan Aug 31 '16 at 08:23
  • @peter-pan-msft Azure ASM has a different concept. It's possible in ASM as you suggested but i am looking for Azure ARM SDk. – Pawan Kumar Aug 31 '16 at 08:40
  • @PawanSharma Sure. It may be your want that using `Template deployment` to delete resources, please refer to the REST API [`Delete a template deployment`](https://msdn.microsoft.com/en-us/library/azure/dn790545.aspx). Hope it helps. – Peter Pan Aug 31 '16 at 10:30
0

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

Pawan Kumar
  • 1,511
  • 4
  • 15
  • 18
0

You need to add the Delete options when creating the Virtual Machine.

Example:

azureResourceManager.virtualMachines().define(linuxVMName)
                .withRegion(Region.GERMANY_WEST_CENTRAL)
                .withExistingResourceGroup(myResourceGroup)
                .withExistingPrimaryNetworkInterface(myNetworkInterface)
                .withGeneralizedLinuxCustomImage(myImageId)
                .withRootUsername(myUsername)
                .withSsh(mySshPublicKey)

                //deleteOptions for related disc
                .withOSDiskDeleteOptions(DeleteOptions.DELETE)

                //deleteOptions for related network interface
                .withPrimaryNetworkInterfaceDeleteOptions(DeleteOptions.DELETE)
                .withSize(VirtualMachineSizeTypes.STANDARD_B2MS)
                .create();
Bilal Demir
  • 587
  • 6
  • 17