51

Is it possible to make a batch file delete itself?

I have tried to make it execute another file to delete it but this did not work.

Does any one know how I could do it?

The batch file I am using is elevated. My OS is Windows 7 32 bit.

10 Answers 10

86

npocmaka's answer works, but it generates the following error message: "The batch file cannot be found." This isn't a problem if the console window closes when the script terminates, as the message will flash by so fast, no one will see it. But it is very undesirable if the console remains open after the script terminates.

The trick to deleting the file without an error message is to get another hidden process to delete the file after the script terminates. This can easily be done using START /B to launch a delete process. It takes time for the delete 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

You can simply use a CALLed subroutine if you are worried about SHIFT trashing the %0 value.

call :deleteSelf&exit /b
:deleteSelf
start /b "" cmd /c del "%~f0"&exit /b

Update 2015-07-16

I've discovered another really slick way to have a batch script delete itself without generating any error message. The technique depends on a newly discovered behavior of GOTO (discovered by some Russians), described in English at http://www.dostips.com/forum/viewtopic.php?f=3&t=6491

In summary, (GOTO) 2>NUL behaves like EXIT /B, except it allows execution of concatenated commands in the context of the caller!

So all you need is

(goto) 2>nul & del "%~f0"

The returned ERRORLEVEL will be 0 because DEL is the last command and it always clears the ERRORLEVEL.

If you need to have control of the ERRORLEVEL, then something like

(goto) 2>nul & del "%~f0" & cmd /c exit /b 10
8
  • 1
    ISTM that this works fine without the parentheses; i.e., goto 2> nul & … Commented Jun 23, 2018 at 21:03
  • @Scott I concur. It seems to work fine without parens. I actually just created a batch file that creates another batch file that then deletes itself after it finishes all its work! Commented Sep 14, 2020 at 13:28
  • @dbenham Any reason to believe that the parens around goto are needed or that omitting them will cause any issues? Commented Sep 14, 2020 at 13:29
  • 2
    @RockPaperLz-MaskitorCasket - It should work just fine without the parens. I include them to make it obvious that I intentionally did not include a target for the GOTO.
    – dbenham
    Commented Sep 14, 2020 at 15:31
  • 1
    Hi i have a problem - i used goto 2>nul & del "%~f0" and my cmd window isnt closing automatically after the script finishes. If I use exit /b only, it will close. If I add an exit /b at the end together with goto 2>nul & del "%~f0", it will not close. Any idea on why is that so and how do I make it close by itself? Thanks
    – Billy Cao
    Commented Nov 8, 2021 at 14:13
9
( del /q /f "%~f0" >nul 2>&1 & exit /b 0  )

Set this at the end of the script. (might not work if SHIFT command is used)

3
  • 2
    This works, but it generates "The batch file cannot be found." error. See my answer for a solution.
    – dbenham
    Commented Dec 2, 2013 at 16:55
  • This lets you return an arbitrary errorcode that the (goto) solution doesn't allow.
    – vvnurmi
    Commented Jun 13, 2018 at 9:45
  • 1
    @vvnurmi - I've updated the (goto) solution to show how to return your own errorcode.
    – dbenham
    Commented Sep 16, 2019 at 13:26
5
del "%~f0"

will work, but there's an error message if you call it from a previously open console (can be ignored however).

4
del %0

As the last line of the file is the easiest way I've found. %0 returns the name of the currently executing .cmd file.

When I first tried it, I thought I would get a "file in use" error, but that hasn't happened so far.

1

del %0 as the last line of the batch file

0
1

As an answer to this question, (which is put on hold) this batch will selfdestruct after three runs. The code to achieve this is condensed in the last 3 lines. It incorporates the above tip from dbenham

@Echo off
Rem batch main purpose
Rem
Rem
Set Tok=###&For /f %%C in ('find /C "%Tok%" ^<"%~f0"') Do Set /A Cnt=%%C
Echo Run-%Cnt%&If %Cnt% Geq 3 (goto) 2>nul & del "%~f0"
Echo %Tok%>>"%~f0"& Goto :Eof

The batch modifies itself by appending a token after each run and counting the occurences of the token. If the number is reached it deletes itself.

> Dir /B Kill*
KillMySelf.cmd

> KillMySelf.cmd
Run-1

> KillMySelf.cmd
Run-2

> KillMySelf.cmd
Run-3

> Dir /B Kill*
File Not Found
0

You can use del (name) if it on the desktop otherwise use del (path to file ex. del C:\WINDOWS.

P.S Does not make error msg

0
0

you can set the delay to avoid race conditions, here is 15 seconds :

START /B CMD.EXE /D /C "PING.EXE -n 15 127.0.0.1 && DEL prova.bat"

NOTES

  • CMD.EXE /D is mandatory to avoid execution of auto-run crap-ware
  • PING and DEL must be executed syncronous, so surrounded by quotes
  • I let to the users how to pass the batch name using "%~f0"
1
  • You can also use the TIMEOUT command to make it wait and make it not so jarring for regular users. You can also press a button to end it ahead of time. Note that it only works with Windows 7 and newer.
    – MDuh
    Commented Jun 6, 2019 at 18:06
0

In case someone else was trying to use user6811411's 3 run and then self destruct script and it was not working. I figured out the &'s were not being interpreted right. My solution was just to separate the commands like below.

@Echo off
Rem batch main purpose
Rem
Rem
Set Tok=###
For /f %%C in ('find /C "%Tok%" ^<"%~f0"') Do Set /A Cnt=%%C
Echo Run-%Cnt%
If %Cnt% Geq 3 (goto) 2>nul & del "%~f0"
Echo %Tok%>>"%~f0" & Goto :Eof
0

this one is really easy. try this:

@echo off
del /Q batchfilename.bat

(The /Q is so you don't have to confirm to delete the file.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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