Click here to Skip to main content
13,293,474 members (45,045 online)
Click here to Skip to main content
Add your own
alternative version


61 bookmarked
Posted 26 Apr 2003

Writing a self destructing exe file

, 26 Apr 2003
Rate this:
Please Sign up or sign in to vote.
Explains how you can have a program delete itself once it has finished running without a reboot


Uninstall programs typically want to delete themselves at the end of the un-installation, but executable cannot delete itself by simply calling the DeleteFile function. By calling the Selfdestruct() function showed below before program exit, the calling executable will be destroyed as soon as possible. The method shown in this article works on all Win32 platforms and there is no need to reboot.

Using the code

Just call the Selfdestruct() function before program exit.

// this is the name of the temporary .bat file
static const char tempbatname[] = "_uninsep.bat" ;

void Selfdestruct() 
  // temporary .bat file
  static char templ[] = 
    "del \"%s\"\r\n"
    "if exist \"%s\" goto Repeat\r\n"
    "rmdir \"%s\"\r\n"
    "del \"%s\"" ;

  char modulename[_MAX_PATH] ;    // absolute path of calling .exe file
  char temppath[_MAX_PATH] ;      // absolute path of temporary .bat file
  char folder[_MAX_PATH] ;

  GetTempPath(_MAX_PATH, temppath) ;
  strcat(temppath, tempbatname) ;

  GetModuleFileName(NULL, modulename, MAX_PATH) ;
  strcpy (folder, modulename) ;
  char *pb = strrchr(folder, '\\');
  if (pb != NULL)
    *pb = 0 ;

  HANDLE hf ;
  hf = CreateFile(temppath, GENERIC_WRITE, 0, NULL, 
    DWORD len ;
    char *bat ;

    bat = (char*)alloca(strlen(templ) + 
               strlen(modulename) * 2 + strlen(temppath) + 20) ;

    wsprintf(bat, templ, modulename, modulename, folder, temppath) ;

    WriteFile(hf, bat, strlen(bat), &len, NULL) ;
    CloseHandle(hf) ;

    ShellExecute(NULL, "open", temppath, NULL, NULL, SW_HIDE);

How it works

Let's assume the executable that wants to destroy itself is located in c:\myfolder\selfdestruct.exe. The Selfdestruct() function will create following .bat in the computers temp folder and then launches it:

del "c:\myfolder\selfdestruct.exe"
if exist "c:\myfolder\selfdestruct.exe" goto Repeat
rmdir "c:\myfolder"
del "c:\temp\_uninsep.bat" ;

The .bat file will try to delete the c:\myfolder\selfdestruct.exe over and over until it finally succeeds (that is as soon as selfdestruct.exe has finished execution. Then it tries to remove the containing folder (here c:\myfolder) which will work only if it is empty and finally deletes itself. Fortunately .bat files can delete themselves.


This article, along with any associated source code and files, is licensed under A Public Domain dedication


About the Author

Michael Walz
Switzerland Switzerland
No Biography provided

You may also be interested in...


Comments and Discussions

Generalfolder doesnot get deleted Pin
learningvisualc18-Mar-10 1:04
memberlearningvisualc18-Mar-10 1:04 
QuestionWhat is this templ????????????? Pin
RAJEEV_RSD25-Sep-09 0:05
memberRAJEEV_RSD25-Sep-09 0:05 
AnswerRe: What is this templ????????????? Pin
Michael Walz25-Sep-09 0:44
memberMichael Walz25-Sep-09 0:44 
Generalbat file Pin
tetadenovicia26-Jun-08 8:09
membertetadenovicia26-Jun-08 8:09 
QuestionSelf-Destructing Files Pin
tao17-May-08 11:56
membertao17-May-08 11:56 
GeneralHere's another safe and simple way Pin
Paul Sanders (AlpineSoft)20-Oct-07 8:02
memberPaul Sanders (AlpineSoft)20-Oct-07 8:02 
QuestionIssue with deleting the directory Pin
ccoffman9729-Dec-06 6:52
memberccoffman9729-Dec-06 6:52 
AnswerRe: Issue with deleting the directory Pin
SF123420-Feb-07 17:43
memberSF123420-Feb-07 17:43 
GeneralRe: Issue with deleting the directory Pin
ccoffman9721-Feb-07 4:09
memberccoffman9721-Feb-07 4:09 
QuestionModification Question Pin
Dark_Xion1-Oct-06 7:51
memberDark_Xion1-Oct-06 7:51 
GeneralI wrote something like this... Pin
Khan Shere18-Jan-05 22:22
memberKhan Shere18-Jan-05 22:22 
GeneralRe: I wrote something like this... Pin
Khan Shere21-Jul-05 8:59
memberKhan Shere21-Jul-05 8:59 
GeneralBug fixes and enhancements Pin
Dennis Jiang4-Jul-03 10:37
memberDennis Jiang4-Jul-03 10:37 
GeneralRe: Bug fixes and enhancements Pin
amethystwu24-Oct-05 21:23
memberamethystwu24-Oct-05 21:23 
GeneralOther methods Pin
Papa28-Apr-03 4:34
memberPapa28-Apr-03 4:34 
GeneralRe: Other methods - Clickety Pin
Phil J Pearson29-Apr-03 8:06
memberPhil J Pearson29-Apr-03 8:06 
GeneralRe: Other methods: More Methods Pin
DoctorKoch15-Jan-07 0:40
memberDoctorKoch15-Jan-07 0:40 
GeneralReally really small article HTML bug Pin
Dominik Reichl27-Apr-03 10:26
memberDominik Reichl27-Apr-03 10:26 
GeneralBatch doesn't terminate Pin
Dominik Reichl27-Apr-03 10:16
memberDominik Reichl27-Apr-03 10:16 
GeneralRe: Batch doesn't terminate Pin
jlap77727-Apr-03 14:45
memberjlap77727-Apr-03 14:45 
GeneralRe: Batch doesn't terminate Pin
Dominik Reichl28-Apr-03 7:45
memberDominik Reichl28-Apr-03 7:45 
GeneralRe: Batch doesn't terminate Pin
DJWALSH28-Apr-03 3:08
memberDJWALSH28-Apr-03 3:08 
GeneralRe: Batch doesn't terminate Pin
Dominik Reichl28-Apr-03 7:43
memberDominik Reichl28-Apr-03 7:43 
GeneralRe: Batch doesn't terminate Pin
Michael Walz28-Apr-03 7:50
memberMichael Walz28-Apr-03 7:50 
GeneralRe: Batch doesn't terminate Pin
Dominik Reichl28-Apr-03 8:49
memberDominik Reichl28-Apr-03 8:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 27 Apr 2003
Article Copyright 2003 by Michael Walz
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid