Search code examples
batch-fileprocessdelete-file

How to make .BAT file delete it self after completion?


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.


Solution

  • @ECHO OFF
    SETLOCAL
    
    SET someOtherProgram=SomeOtherProgram.exe
    TASKKILL /IM "%someOtherProgram%"
    
    ECHO "This script will now self-destruct. Please ignore the next error message"
    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 :)

    This will print out an ugly error message, but it is benign, and the code is slightly less confusing this way. If you care a lot about getting rid of the error message, see dbenham's answer to get rid of it.