up vote 4 down vote favorite
5

Is there any way that a running process can delete its own executable?

For example, I make a console application (single exe) and after doing some tasks it somehow deletes the exe file.

I have to send a single file to someone. And I want it deleted after it does its intended task.

Is there anyway to do it in Windows

flag

I found out what most suited me in the article catch22.net/tuts/selfdel. – cornerback84 Oct 26 '09 at 5:48

7 Answers

up vote 2 down vote accepted

I found two articles here which can solve your problem

http://www.catch22.net/tuts/selfdel

http://blogorama.nerdworks.in/comment.aspx?entryID=21

link|flag
catch22.net/tuts/selfdel is a good article. Solved my problem. – cornerback84 Oct 26 '09 at 5:47
up vote 4 down vote

One way to do this is to use the MoveFileEx function with the MOVEFILE_DELAY_UNTIL_REBOOT flag and a NULL destination. According to the documentation, this:

registers the lpExistingFileName file to be deleted when the system restarts. If lpExistingFileName refers to a directory, the system removes the directory at restart only if the directory is empty.

link|flag
I do not understand why we can't delete it because exe gets loaded to RAM ,so why does it require to communicate with hard drive ? – Xinus Oct 22 '09 at 10:10
1  
Windows only loads as much of the executable as it needs to, so that it doesn't (a) use up too much RAM or (b) have to write the executable out to the swap file again if there isn't enough RAM. You can think of each executable being run by Windows as a temporary read-only swap file. If a page of RAM needs to be discarded, Windows can simply discard it knowing that it can always be reloaded from the EXE file when necessary. – Greg Hewgill Oct 22 '09 at 10:16
.....thanks..... – Xinus Oct 22 '09 at 18:51
This method is too reliant on restart. No doubt it would work, but I wanted to delete the file as soon as I can. – cornerback84 Oct 26 '09 at 5:51
up vote 2 down vote

Check out the similar stack discussion Self deletable application in C# in one executable

link|flag
up vote 1 down vote

It's possible to do this on Linux. You'll find that it is generally not possible to delete a running executable on Windows. However, you can have Windows delete the EXE for you on the next reboot: http://www.howtodothings.com/computers/a1402-delete-a-running-exe.html

If you want the file deleted after it's been run, you could simply ask the user to delete it. If the reason you want this is as a security measure, then what you're doing is misguided. The user could circumvent this by simply making a copy of the file first.

link|flag
up vote 0 down vote

You can use windows scheduler to schedule a task to delete your program after X seconds.

Command line: http://msdn.microsoft.com/en-us/library/bb736357%28VS.85%29.aspx

Or API: http://msdn.microsoft.com/en-us/library/aa383608%28VS.85%29.aspx

link|flag
up vote 0 down vote

You can run another application, which would wait for parent process to terminate, and then delete its executable.

link|flag
up vote 0 down vote

Until that exe is in memory, it will not be able to delete itself. However, it can register with the system a task for deleting itself after a set time period of gap when it would be expected to be completing its execution.

link|flag

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.