Server 2012 R2 - how to delete a file that is "open in System"?
I am using Server 2012 R2 Datacenter Core. It is one of our Hyper-V hosts. I manage it from my workstation using Hyper-V manager, and manipulate files through the admin share.
I created a VM, then copied a VHDX into the Hard Disks folder. I then attempted to start the VM with that VHDX, but (since it is an old image and asked for an Admin password I don't have), I decided I would build from scratch. I shut down the VM, removed the drive, and attempted to delete the VHDX so I could create a new one with the same name. I get "The action can't be completed because the file is open in System". This has been happening for several hours. I initially suspected our AV solution, but it can't be taking this long to scan a 17GB file (I think).
Now, I've had this happen on Win7 systems before, and there's a plethora of methods for forcing a file to be deleted. In this case though, I can't just go to the host and boot it offline, or install SuperKillerFileDeleterUltraGuaranteedBox.
So how do I get rid of it?
P.S. I also realize I could just name the new VHDX something else - it doesn't have to be the same as the VM name. But that's just insulting, and anyway I can't just leave an unused 17GB file on the server forever.
Yeah, I've run into this. It's very likely that Hyper-V is holding the file. You'll probably have to stop the Hyper-V Virtual Machine Management service in order to delete the VHD. Either that or reboot the Hyper-V host. Alternatively, you might check to see if there's a Virtual Machine Worker Process that corresponds to the deleted VM. If there is you can kill that which should then allow you to delete the VHD.
You can run this in Powershell to see if there's a process ID "GUID" that corresponds to a Virtual Machine Worker Process (found by right clicking the Virtual Machine Worker Process in Task Manager, selecting "Go to details", and looking at the username "GUID"). If you find a matching process for the deleted VM then kill that process and delete the VHD.
Get-WmiObject -Namespace root\virtualization -class msvm_computersystem | select elementname, operationalstatus, processid, name| ft -auto
Found this elsewhere (not my own work, comment from techienateafs on spiceworks) and it worked for me. I intermittently hit this after mounting and then dismounting a virtual disk (via powershell Mount-WindowsImage and Dismount-WindowsImage) due to the dismount failing.
"I was able to solve the problem by opening diskmgmt.msc on the host server. The disk was listed there, right click and choose detach. It prompts for confirmation that you have the correct file. At that point, process explorer confirms that the file is no longer open by pid 4 (nt kernel & system) and I am able to work freely with the .vhdx file."
Well, this is super embarrassing for my first post to serverfault... Apparently, it was my System that had a handle on the file. I rebooted my local machine and voila was able to delete the file. I can't believe I didn't try that first.