VMware Cloud Community
mohd1024
Contributor
Contributor

How to delete a virtual machine?

Hello All,

Iam developing an application in C# in which I have to delete a virtual machine from ESX server.

Is there a direct way to delete a VM using its inventory path , or should I first get the path of its vmx file and then use the API "DeleteFile".

If I have to use the "DeleteFile", the first Argument it takes is a managedobjectreference ot the HostDatastoreBrower used to make the call.

Where can I get this reference?

Thanks a lot.

0 Kudos
3 Replies
mohd1024
Contributor
Contributor

I have use the following to get the name of the file

VM.config.files

And I used the following to get the Hostdatastorebrowser

VM.environmentBrowser.hostdatastorebrowser

but after executing the API "DeleteFile" only the .vmx file was deleted and the virtual machine still can be seen in the Inventory.

How can I delete a Virtual Machine?

0 Kudos
virtualdud3
Expert
Expert

...

Message was edited by:

virtualdud3

############### Under no circumstances are you to award me any points. Thanks!!!
0 Kudos
mohd1024
Contributor
Contributor

Thanks All,

I have solved it.

You can delete a Virtual Machine in c# by using the following API.

service.DestroyTask(ManagedObjectReference VM);

Where VM is a reference to the Virtual Machine you want to delete you can get this reference by the following.

string PathToVm="\[Datacenter]/vm/\[VM Name]";

VM=_service.FindByInventoryPath(_sic.searchIndex, PathToVm);

Best Wishes.

0 Kudos