How to Delete Virtual Machine With vSphere API?

This question was asked at VI Java API forum recently and has been answered by the community.

There should be a “Destroy_Task()”-Method for each Managed Object, so also Virtual Machines…. Cheers, Joerg

Lost VMs or Containers? Too Many Consoles? Too Slow GUI? Time to learn how to "Google" and manage your VMware and clouds in a fast and secure HTML5 App.

As  Jörg Lew has correctly pointed out the solution, I would like to elaborate a bit more here.

If you are using VI Java API, the method name is destroy_Task(). The code is as simple as:

VirtualMachine vm = null;
//get object assigned to vm
vm.destroy_Task()

To run the code, the user must have “VirtualMachine.Inventory.Delete” privilege on the virtual machine object. For other types of objects, the required privileges are different even though the method called is exactly the same. For the data center, for example, it’s “Datacenter.Delete.” You can check out the full table in the API reference.

Using destroy_Task() method can be a dangerous because it deletes everything of a VM, which cannot be reversed. If you call the method by mistake, then you’re screwed.

To play safe, you have another method to call: unregisterVM() method defined in VirtualMachine. When it’s called, the virtual machine disappears from the inventory tree, but all the files are still there. If you regret what you just called, you can add the virtual machine any time by calling the registerVM_Task() defined in Folder managed object.

The downside of this approach is you haven’t really released the disk space. If you have many of such virtual machines, you may find your data store out of space easily. To deal with it, you can run a script that scan for unregistered VM and remove them once in a while.

In the end, I have a fun question for you: what happens if you call destroy_Task() on a HostSystem? Will it destroy your ESX or ESXi?

This entry was posted in vSphere API and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

3 Comments

  1. Posted January 18, 2011 at 5:33 am | Permalink

    For the last question:
    I think, disabling HostSystem.config.service.service[“DCUI”] and HostSystem.enterLockdownMode() will do the job if you loose the connection between vCenter and ESXi permanently :-)
    Cheers,
    Joerg

  2. timo2145
    Posted April 2, 2014 at 1:24 pm | Permalink

    Leaves an orphaned server in the VCenter. Why does this code suck so bad.

  3. Posted April 2, 2014 at 2:45 pm | Permalink

    That is probably you delete a VM by running the code with ESX(i) without knowledge of vCenter. Next time, connect to vCenter.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  • NEED HELP?


    My company has created products like vSearch ("Super vCenter"), vijavaNG APIs, EAM APIs, ICE tool. We also help clients with virtualization and cloud computing on customized development, training. Should you, or someone you know, need these products and services, please feel free to contact me: steve __AT__ doublecloud.org.

    Me: Steve Jin, VMware vExpert who authored the VMware VI and vSphere SDK by Prentice Hall, and created the de factor open source vSphere Java API while working at VMware engineering. Companies like Cisco, EMC, NetApp, HP, Dell, VMware, are among the users of the API and other tools I developed for their products, internal IT orchestration, and test automation.