# Write a program that deletes itself [duplicate]

For this code golf challenge, you must produce a .exe runnable on windows. After running this .exe from any path with any name it must be deleted after execution.

My reason for limiting this to windows is something like this may be significantly easier if you have some niche OS.

Language(and code) used to produce the .exe is irrelevant, the file size of the executable is what will be measured for this. But still post the code used to produce it, along with the resulting file size.

This is code golf, but again the goal is the smallest executable file.

• @ArlaudPierre Well, I had to pick one, and (unfortunately) I have more experience with it. Jan 3, 2014 at 16:15
• Must the program delete only itself, or can it nuke, say, lots of other things as well? Jan 4, 2014 at 1:50
• @ArlaudPierre Also, notably, windows does not allow the deletion of a program while it is running. This increases the challenge level a bit. Jan 4, 2014 at 4:28
• Linux is totally a niche OS, where main(int a,char**b){unlink(*b);} is all you need. (Same goes for OSX.) Jan 4, 2014 at 4:33
• @JanDvorak This is a lot like copying someone's song/book/movie, changing a couple of words and the title, and then suing them for copying you. At the very least this should go through meta first to discuss this course of action, in my opinion. Jan 25, 2014 at 10:18

## Assembly, 4608 bytes

The source code for the assembly program, using nasm syntax, is as follows:

BITS 32

GLOBAL _main
EXTERN _system

SECTION .data
cmd:    db      'echo>).bat start cmd /c del ).bat '
cmdarg: times 128 db 0

SECTION .text

_main:
mov     eax, [esp + 8]
mov     esi, [eax]
mov     edi, cmdarg
copy:   lodsb
or      al, al
stosb
jnz     copy
push    cmd
call    _system
call    _system
pop     edx
ret


I built the executable using the following commands:

nasm -f win32 selfdel.asm
gcc -Wall -s -o selfdel.exe selfdel.obj


(I'm curious if the size of the executable would be different if MS's linker were to be used in place of gcc's, but I don't have access to MS development tools.)

Hopefully the code is straightforward enough to not need much explanation. Basically the program creates a batch file that deletes the program (as well as itself) after the program has exited.

# Perl (11 bytes)

MZ+unlink\$0


Save this as a.exe, or something, and run it with Perl. And yes, this is Win16 executable, except I don't know what it will do.

• "And yes, this is Win16 executable" I do not believe this is true. I believe a Win16 binary needs to be an NE executable, in turn a subformat of an MZ executable, which needs to start with the two magic bytes MZ. Jan 4, 2014 at 19:20
• @nitro2k01: Added two magic bytes at the beginning. Jan 4, 2014 at 19:22
• "Win16 executable, except I don't know what it will do" I love it, this needs more votes
– cat
Mar 4, 2016 at 12:17

## Tcl, 48

exec cmd /c "ping ::1&&del [file na [info n]]" &


Use a tclsh basekit to create the executable.

Note: a process can not delete its own executable, because it is locked when the process is running. So your only option is to get some other process to delete your executable when it stops running.

• The final count goes by executable size, not source size Jan 4, 2014 at 3:35
• I think I should use C for that. A simple system call should do it. The entire Tcl runtime is a little bit big with 2.3MB. Jan 4, 2014 at 3:50
• codegolf.stackexchange.com/a/123067/29325 May 27, 2017 at 8:36

# C - 857 byte source, 18,432 byte compilation

#include <stdlib.h>

size_t stringlen(const char *);
char * stringcat(char *, const char *);

main(int argc, char *argv[])
{
if(argc > 0)
{
char *str = calloc(stringlen(argv[0])+11, sizeof(char));
stringcat(str, "start del ");
stringcat(str, argv[0]);
system(str);
}
}

/* Simple implementation of strcat to avoid including string.h */
char * stringcat(char *dest, const char *source)
{
size_t i=0,j=0;
while(dest[i] != '\0')
{
++i;
}

while(source[j] != '\0')
{
dest[i+j] = source[j];
++j;
}
dest[i+j] = '\0';
return dest;
}

/*Simple implementation of strlen to avoid including string.h */
size_t stringlen(const char *str)
{
size_t i=0;
while(str[i] != '\0')
{
++i;
}
return i;
}


Compiled with:

gcc self-delete.c -pedantic -s -o self-delete.exe


It is now tested and works perfectly.

• rm on windows?? Jan 4, 2014 at 3:12
• @SztupY Sorry, I was not thinking :P Jan 4, 2014 at 3:30

# Python (59 bytes)

import os,sys;os.remove(sys._getframe().f_code.co_filename)


## Assembly - 1548 bytes

Executes a delayed system command to find and delete itself

EXTERN system
IMPORT system msvcr100.dll

SECTION .text USE32
GLOBAL main
main:
push string
call [system]
ret

string:
db "start cmd /C ", 34, "timeout /t 3&for /f %a in ('dir /b *.exe') do (find ", 34, "someuniquestring134123asdfasd41324", 34,  " %a &if not errorlevel 1 del %a)", 34, 0


Compiled with:

nasm.exe -f obj filename.asm


The executed script:

timeout /t 3
for /f %a in ('dir /b *.exe') do (
find "someuniquestring134123asdfasd41324", %a
if not errorlevel 1 del %a
)


## HTML + JS (33)

<body onload=document.write('')>

• “you must produce a .exe runnable on windows. After running this .exe from any path with any name it must be deleted after execution.” Not even close.
– Ry-
Jan 31, 2014 at 3:47