Take the 2-minute tour ×

How to make .BAT file delete it self after completion? I have a simple bat file that terminates a process. I want that .BAT file to delete itself

share|improve this question
So you want a batch file that doesn't show any windows, kills a process and deletes itself after completion. Just out of curiosity. Why? –  Martin Smith May 22 '10 at 17:31
add comment

4 Answers

up vote 22 down vote accepted
SET someOtherProgram=SomeOtherProgram.exe
TASKKILL /IM "%someOtherProgram%"
DEL "%~f0"

Note that the DEL line better be the last thing you intend to execute inside the batch file, otherwise you're out of luck :)

[Edit: Missed the "killing other process" part - my batch file originally launched a process]

share|improve this answer
Can you explan this a little bit please? What is someotherprogram.exe and what do you punt inside it? –  Ricardo Polo Oct 10 '11 at 21:05
@Richard: SomeOtherProgram.exe could be changed to be any program. For example, notepad.exe. This is the program that will be killed, and it doesn't matter what that program does - this batch file doesn't care. It just uses TaskKill to kill a task with that exe name. Does this clear it up? If so, did you think it needs to be added to the answer? If not, please explain in more detail what parts you understand, and which you don't. –  Merlyn Morgan-Graham Oct 11 '11 at 0:17
Thank you for answer @Merlyn. My fault, I didnt read well the question. I was figuring why do you need to kill a process to delete a bat itself. :) –  Ricardo Polo Oct 11 '11 at 0:28
The batch file will delete itself just fine, but an ugly "The batch file cannot be found." error message is generated. See my answer for a method to delete without error. –  dbenham Dec 6 '13 at 15:29
add comment

The Merlyn Morgan-Graham answer manages to delete the running batch script, but it generates the following error message: "The batch file cannot be found." This is not a problem if the console window closes when the script terminates, as the message will flash by so fast that no one will see it. But the error message is very undesirable if the console remains open after script termination.

John Faminella has the right idea that another process is needed to cleanly delete the batch file without error. Scheduling a task can work, but there is a simpler way: use START to launch a new delete process within the same console. It takes time for the process to initiate and execute, so the parent script has a chance to terminate cleanly before the delete happens.

start /b "" cmd /c del "%~f0"&exit /b
share|improve this answer
add comment

You didn't mention the OS, but if this is on Windows XP Professional and you have the appropriate permissions, you can have the batch file schedule a one-shot Windows Scheduled Task to delete the file at a later time. Use the schtasks command, documented here.

Otherwise, you typically can't delete a file that is being executed, since that has the potential for all sorts of nastiness. Additionally, trying to delete an executable in use is viewed as very suspicious behavior by any number of antivirus programs, so it's likely that you would run afoul of these as well.

share|improve this answer
Actually, there is a surprisingly simple way to get a batch file to delete itself without error that does not require special permissions. See my answer –  dbenham Dec 6 '13 at 15:26
add comment

Just add this command at the last line of your batch file

Del batch_file_name.bat

batch_file_name.bat is the name of your batch file


share|improve this answer
It does not work. –  Ricardo Polo Oct 10 '11 at 21:03
add comment

Your Answer


