/// <summary>
/// Deletes the virtual machine that has context with the client
/// </summary>
/// <param name="removeDisks">True if the underlying disks in blob storage should be removed</param>
/// <param name="removeUnderlyingBlobs">Whether or not remove the blob as well as the OS disk</param>
/// <param name="removeCloudService">Removes the cloud service container</param>
/// <param name="removeStorageAccount">The storage account that the vhd is in</param>
public void DeleteVirtualMachine(bool removeDisks = true, bool removeUnderlyingBlobs = true, bool removeCloudService = true, bool removeStorageAccount = true)
{
// set this if it hasn't been set yet
PersistentVMRole vm = null;
if (_vmRole == null)
{
vm = VirtualMachine;
}
// create the blob client
string diskName = _vmRole.OSHardDisk.DiskName;
string storageAccount = ParseBlobDetails(_vmRole.OSHardDisk.MediaLink);
// create the blob client
IBlobClient blobClient = new BlobClient(Properties.SubscriptionId, StorageContainerName, storageAccount, Properties.Certificate);
// first delete the virtual machine command
var deleteVirtualMachine = new DeleteVirtualMachineCommand(Properties)
{
SubscriptionId = Properties.SubscriptionId,
Certificate = Properties.Certificate
};
try
{
deleteVirtualMachine.Execute();
}
catch (Exception)
{
// should be a 400 here if this is the case then there is only a single role in the deployment - quicker to do it this way!
var deleteVirtualMachineDeployment = new DeleteVirtualMachineDeploymentCommand(Properties)
{
SubscriptionId = Properties.SubscriptionId,
Certificate = Properties.Certificate
};
deleteVirtualMachineDeployment.Execute();
}
// when this is finished we'll delete the operating system disk - check this as we may need to putin a pause
// remove the disk association
DeleteNamedVirtualMachineDisk(diskName);
// remove the data disks
DeleteDataDisks(removeDisks ? blobClient : null);
if (removeDisks)
{
// remove the physical disk
blobClient.DeleteBlob(StorageFileName);
}
// remove the cloud services
if (removeCloudService)
{
// delete the cloud service here
var deleteCloudService = new DeleteHostedServiceCommand(Properties.CloudServiceName)
{
SubscriptionId = Properties.SubscriptionId,
Certificate = Properties.Certificate
};
deleteCloudService.Execute();
}
// remove the storage account
if (removeStorageAccount)
{
blobClient.DeleteStorageAccount();
}
}