How can a program delete its own executable How can a program delete its own executable windows windows

How can a program delete its own executable


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.


process.start("cmd /c ping localhost -n 3 > nul & del filepath")exit

Explanation :

ping localhost -n 3

Adds a slight delay before executing del filepath. By the time it's triggered, your program has exited.

Replace process.start with whatever command your programming language uses to start programs with arguments.

Replace filepath with the path to your exe.

Replace exit with the command for terminating your program.