diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm index 267a08e8..7e1d3147 100644 --- a/PVE/API2/Qemu.pm +++ b/PVE/API2/Qemu.pm @@ -1492,9 +1492,15 @@ __PACKAGE__->register_method({ my $upid = shift; syslog('info', "destroy VM $vmid: $upid\n"); - PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock); - PVE::AccessControl::remove_vm_access($vmid); - PVE::Firewall::remove_vmfw_conf($vmid); + PVE::QemuConfig->lock_config($vmid, sub { + die "VM $vmid is running - destroy failed\n" + if (PVE::QemuServer::check_running($vmid)); + PVE::QemuServer::destroy_vm($storecfg, $vmid, 1, $skiplock); + PVE::AccessControl::remove_vm_access($vmid); + PVE::Firewall::remove_vmfw_conf($vmid); + unlink PVE::QemuConfig->config_file($vmid) + or die "Removal of VM $vmid config file failed: $!\n"; + }); }; return $rpcenv->fork_worker('qmdestroy', $vmid, $authuser, $realcmd);