Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Everything is in the title, but more officially :

Why does Windows let me rename a running executable, but not delete it?

share|improve this question
up vote 8 down vote accepted

There really is no such thing as renaming a file. A file can have more than one name or no name, so it's not the file that you're renaming but the directory entry. Renaming is an operation on the directory entry, which is not affected by the fact that the file is locked for execution.

share|improve this answer
Hmm, why then any attempt to rename a regular file that is open for reading or writing fails? – Serge Oct 15 '12 at 20:25
@Serge: Because the process that opened the file specifically asked for it to fail by setting the appropriate open flags. – David Schwartz Oct 15 '12 at 20:41
what specific open flags? – n611x007 Aug 29 '13 at 19:23
Most likely, setting dwShareMode to zero or using the OF_SHARE_COMPAT or OF_SHARE_EXCLUSIVE flags. – David Schwartz Aug 29 '13 at 19:40

It does not allow to delete the executable file and DLLs because Windows maps parts of the executable files into memory as part of the process creation, so it need the file during the lifetime of the process.

Unfortunately I have no true reason why it still allows to rename such files. I guess that this is done to enable the update of the dlls and exe files while they are running to minimize the service interruption time.

The linux (unix in general) in contrast allows to delete an executable file while it is running:

tmp]$ cp /usr/bin/md5sum .;ll md5*; \
(./md5sum /home/pub/iso/FC5/FC-5-i386-DVD.iso & ); \
rm md5sum ; ll md5*;ps -f
-rwxr-xr-x 1 sergey sergey 37276 Oct 16 02:38 md5sum
ls: cannot access md5*: No such file or directory
sergey    2423  2422  0 02:32 pts/1    00:00:00 -bash
sergey    2533     1  0 02:38 pts/1    00:00:00 ./md5sum /home/pub/iso/FC5/FC-5-
sergey    2536  2423  0 02:38 pts/1    00:00:00 ps -f
share|improve this answer
Note that e.g. Linux also maps parts of the executable files into memory, but has no problem allowing you to delete a running executable. – ChrisInEdmonton Oct 15 '12 at 20:31
@ChrisInEdmonton Yes, but this I explained here:… – Serge Oct 15 '12 at 20:32
Serge, nice explanation there. :) – ChrisInEdmonton Oct 15 '12 at 21:56
Linux will not allow you to delete a file while it is executing. You can, however, remove directory entries, since those are not executing. – David Schwartz Oct 15 '12 at 22:10
@DavidSchwartz please see the update to my answer. Linux does allow me to unlink any file being executed provided I have enough permissions to delete that file. – Serge Oct 15 '12 at 22:43

I guess it is because a name is just an attribute of the same binary content of the file, so as long as the data is there, the handle, held by the running process for it won't change.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .