How To Make Your Application Delete Itself Immediately

A simple two line technique that can be used in just about any application

Introduction

I'm sure you've all said to yourself or someone at the office at one point or another, "<snort> You idiot. Don't you know a Windows application can't delete itself? I bet you don't even know how to type high ASCII characters using the ALT key and the number pad, gahhhh.."

Sure, there are ways to have a file delete itself on the next reboot... And you can even resort to an external program or batch file to do the work. But I just came up with a nifty way of doing it without leaving a visible trace that the application ever existed!

Background

This was just one of those simple, random ideas that popped up into my head. Took all of about 5 minutes to whip up a quick, working demo application from conception, and the technique is applicable to almost any Windows based development platform/language.

In fact, it's taking me longer to post this on CodeProject! But since I've never posted anything on CodeProject, I thought it'd be a fun, simple exercise. :)

Using the Code

Please note that I cannot possibly post the code for all supported languages/frameworks/platforms where this technique will work. As you will soon see, there are only two lines required to have a program delete itself without leaving a trace. These two lines will probably vary slightly in your development environment of choice, but it shouldn't take more than a minute or two to translate the code to something that works perfect for you.

And here are those two lines of code (as noted by readers, this version doesn't work on XP).

Process.Start(...) is a .NET command that launches an application with parameters. The first parameter is the application you want to launch. The second parameter is a string containing the parameters you want to pass to the application to launch. This command, by default, does not pause and wait for the launched application to finish before moving on to the next command. That next command just happens to be Application.Exit(). In .NET land, that causes the currently running application to completely shutdown. Meanwhile, our command window has popped up and is merely waiting 3 seconds for the calling application to fully close. Once that happens, the Del command comes into play and blows away the application just prior to the command window closing on its own!

The following is a description of what those parameters mean.

Cmd /C causes a command window to appear and run the command specified.. it then causes the window to close automatically.

Choice /C Y /N /D Y /T 3 displays an empty, flashing prompt. However, the /T 3 means that the prompt will automatically select the default choice Y (/D Y) after 3 seconds. I didn't know about the “choice” command until a few minutes before writing this article.

& is used to chain multiple commands together on a single line in a batch file. I didn't know about & until just now either.

Del <Application.ExecutablePath>... Well, I'm sure you can imagine what that does. Everything after the & can be replaced with anything you want to happen after the three second delay. You could have the command delete every file within the directory...or maybe something a little more malicious? :)

This is fairly similar to the previous version except it uses the ping command to do the dirty work. -n 1 tells the command to only ping one time and -w 3000 tells the command to wait 3 seconds before performing the ping. > Nul basically just hides the output of the ping command.

That's all, folks! You can place this code anywhere in your application, including the OnClosed event of your WinForms application.

Points of Interest

Several people have pointed out that this won't work if, for some reason, your application takes longer to shut down than what the timeout period is configured to. If you need a more fool proof technique, you might consider extending the timeout period and/or using the "delete on reboot" registry value.

Also, Filip Geens included code to hide the command prompt window from view...

If you find this code interesting, or if you know of a different technique that has the same benefits as this one, please let me know! Also, if you port this code to another language/platform, please post your version in the comments for others to benefit from.

History

12/2/2008 10:29:11 PM: Finished with version 1.. yee haw!

12/3/2008 10:02:51 AM: Added a version that works with XP and implemented some of the comments from readers

Share

About the Author

Comments and Discussions

If you don't like Cmd.exe there is another option:
You write a little CleanUp.exe that deletes your Uninstaller application (which must have already deleted everything except itslef).
Additionally it deletes the folder in which your application is.

The size of CleanUp.exe is only 6 kB (six kilobyte!)
You copy it into the Temp folder (See MSDN: GetTempPath()) and call it like this:

CleanUp.exe /File=C:\ProgramFiles\MyApplication\Application.exe

(without quotes!!)

Obviously you must call it elevated (AsAdmin). But when the Uninstaller application already runs elevated, CleanUp.exe will also be started elevated.

Then CleanUp.exe waits until your Unistaller application has finished and deletes 'Application.exe' and then removes the folder 'MyApplication'.

Is it possible to add a preset date where the application can delete itself?, and how would I add this to my application, new at this or where would I put the file so that it could be activated on that particular date?
Developed new music software and would like to delete the demo and files after a viewing period

at first very nice article!
it helped me a lot
but there is still one thing i cant get to work:
instead of just deleting the exe itself
i would like to delete the folder it is in
and i tried this: Process.Start("cmd.exe", "/C ping 1.1.1.1 -n 1 -w 3000 > Nul & rmdir /s /q " + Application.StartupPath);
but it just empties the folder instead of removing it
and when i use the command in command line it works perfect
i dont know what could be the problem
any ideas?

You have to put "cd .." before rmdir because the batch file
is executing in the same directory.
Or, if I'm not mistaken set the working directory in a Process.Start parameter
to something other than your application path.

If I want to do something quick and dirty such as patch something, replace something through a small downloadable package then this will provide an elegant solution rather than get the whole package updated via some complicated installer.

For example I have an application that is part of a wider suite of applications, a client just wanted this one and not the full package. He took a copy but it would not run correctly because it needed some registry settings. I sent him the registry settings in a .reg file and that fixed the problem. It did leave the unwanted .reg file around requiring a manual clean up. Usually it makes no sense for just this one application by itself except in this one unique case, so creating a special one off installer for him would be overkill.

I for one am tied of all the crap left on my machines. Its hard to find out what is necessary and what is not. When you install some products, the uninstall does not always clean up completely so one is left with an ever growing quantity of files which probably not needed but one does not want to risk removing just in case.

This solution provides an easy self cleaning solution for situations (mainly one offs) that gets you or your clients out of a hole quickly and cleanly.

Someone said to a search on Google, for better solutions. I did a search on "self deleting on Windows" and only got this one article. I know I could widen the search but if there were bucket loads on the web I would have expected a few more hits.

If nothing else reading the comments from all the naysayers were entertaining.