Skip to content

Instantly share code, notes, and snippets.

@thbishop
Created July 31, 2011 22:23
Show Gist options
  • Save thbishop/1117286 to your computer and use it in GitHub Desktop.
Save thbishop/1117286 to your computer and use it in GitHub Desktop.
How to programmatically delete a VM from VMware Fusion
# The primary fusion app must be closed for this to work.
# It appears that the primary fusion app holds the plist data in memory
# and will write the changes to disk when it closes
# (including overwriting any changes made by another app).
# You should not have any issues if you execute this when there are
# headless VMs running (i.e. 'vmrun start $path_to_vmx nogui').
require 'cfpropertylist'
plist_file_path = '/Users/tbishop/Library/Preferences/com.vmware.fusion.plist'
existing_plist = CFPropertyList::List.new(:file => plist_file_path)
data = CFPropertyList.native_types(existing_plist.value)
vm_name = 'my_test'
vm_path = ''
# get the vm path for later
data['VMFavoritesListDefaults2'].each do |vm|
if vm['name'] == vm_name
vm_path = vm['path']
break
end
end
# this appears to be the list of VM console windows to open when fusion starts
# if there are no VM console windows to open, this key doesn't exist
if data.has_key?('PLRestartDocumentPaths')
data['PLRestartDocumentPaths'].delete_if { |path| path == vm_path }
end
data['VMFavoritesListDefaults2'].delete_if { |vm| vm['name'] == vm_name}
# save the updated data
new_list = CFPropertyList::List.new
new_list.value = CFPropertyList.guess(data)
new_list.save plist_file_path, CFPropertyList::List::FORMAT_BINARY
# I *think* this requires the primary fusion app to be closed.
# This appears to only cleanup the file system and not the plist data.
/Library/Application\ Support/VMware\ Fusion/vmrun -T fusion deleteVM /vm/my_test.vmwarevm/my_test.vmx
@beddari
Copy link

beddari commented Aug 1, 2011

In my testing deleteVM does not work while Fusion is running.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment