Today I’ve come across a weird an annoying problem “a undeletable file” in Windows that was created while using a shell script in Cygwin.
The file had owner: “Unknown+User” and group: “Unknown+Group” making virtually impossible to delete the file without restarting or booting the disk to avoid the file security context.
I tried using several utilities to restore the permissions or delete the file, including:
TAKEOWN /F <filename>
subinacl <filename> /grant=<user>=R
icacls "<filename>" /grant <user>:(OI)(CI)F /T
attrib +r <filename>
psexec -i -s cmd.exe # To become LocalSystem and then DEL (you need to run this as CMD "Administrator")
dir /x # to obtain the 8.3 DOS name and then use takeown
None of them worked, then finally I tried with another sysinternals tool handle which is included in Process Explorer, in recent versions of windows this is available in the process explorer, otherwise you’ll need to download and execute it.
In associated handles I searched part of the file name and found a process:
Finally after killing that process the file got the correct user and group and I was able to delete it: