22
\$\begingroup\$

If a compiled language is used, the program must delete the compiled executable (but need not delete the source file). If an interpreted language is used, the program must delete the source file.

My opening bid:

Python (29 characters)

import os;os.remove(__file__)

Edit: to prevent solutions like rm -rf /, the program must not delete anything except the executable or source file.

\$\endgroup\$
  • \$\begingroup\$ possible duplicate of Write a program that deletes itself \$\endgroup\$ – John Dvorak Jan 25 '14 at 3:39
  • 2
    \$\begingroup\$ @JanDvorak That question was stricter, requiring a .exe file. This is more lax. \$\endgroup\$ – EMBLEM Jan 25 '14 at 3:39
  • \$\begingroup\$ Hmm... what if I vote to close in the opposite direction, then? \$\endgroup\$ – John Dvorak Jan 25 '14 at 3:41
  • 3
    \$\begingroup\$ code-golf, not popularity-contest? Aww. \$\endgroup\$ – Justin Jan 25 '14 at 4:19
  • 10
    \$\begingroup\$ @Quincunx I don't believe in popularity contests. Real code is not first validated on its elegance, but its correctness. \$\endgroup\$ – EMBLEM Jan 25 '14 at 4:24

29 Answers 29

21
\$\begingroup\$

Bash script (7 characters, 7 bytes)

rm "$0"
\$\endgroup\$
  • 1
    \$\begingroup\$ Doesn't work if the script file name contains spaces. \$\endgroup\$ – user80551 Jan 25 '14 at 3:46
  • \$\begingroup\$ Good eye! I've updated the script to work with files with spaces in them. Only adds two more characters. Thanks! \$\endgroup\$ – anthony.c Jan 25 '14 at 3:50
  • \$\begingroup\$ Does this work if the file name contains double quotes? \$\endgroup\$ – John Dvorak Jan 25 '14 at 7:28
  • 1
    \$\begingroup\$ @JanDvorak: Yes. By execution that would be e.g. ./the\"program, ./'the"program', './tha"program'. All works as intended. (Quotes needs to be escaped or enclosed in single quotes on call.) Would also work on files containing names with newline etc. \$\endgroup\$ – Runium Jan 26 '14 at 4:37
  • 1
    \$\begingroup\$ @StéphaneGourichon Probably 'cause he's running rm "$0" in the Bash shell instead of in a batch script. You should edit the header like so: # Bash script, 7 bytes \$\endgroup\$ – MD XF May 30 '17 at 16:33
14
\$\begingroup\$

Unix? (9):

#!/bin/rm

A classic. Uses rm as interpreter, for instant self-deletion. Not the very shortest though.

\$\endgroup\$
  • 4
    \$\begingroup\$ Or if you want to be cheaty: a 0-byte rm program. \$\endgroup\$ – nyuszika7h Jul 2 '14 at 18:07
  • 1
    \$\begingroup\$ The #! line isn't counted in score. \$\endgroup\$ – Pavel May 7 '17 at 3:10
  • \$\begingroup\$ @Phoenix: It is if it's unusual for the language you're using; only completely standard #! lines that just give the name of the language's default interpreter count as zero. This is a very unusual #! line as far as Bash goes. (Not that this actually works in Bash, although there are other languages where it works.) \$\endgroup\$ – user62131 Jun 16 '17 at 6:06
  • 1
    \$\begingroup\$ This isn't actually a Bash program (if you run it explicitly using bash, it won't delete anything). It's a UNIX/Linux executable. (It also works in Perl, which emulates UNIX/Linux's handling of #! lines so that it can be used to run shellscripts on platforms that can't do it natively.) \$\endgroup\$ – user62131 Jun 16 '17 at 6:06
  • \$\begingroup\$ @Pavel Hmm... 0 bytes? \$\endgroup\$ – ɐɔıʇǝɥʇuʎs Sep 11 '17 at 11:46
10
\$\begingroup\$

Batch - 6 bytes

Del %0

Pretty simple. Note: Works even if the name contains spaces.

\$\endgroup\$
  • \$\begingroup\$ Only if the file name doesn't have spaces. \$\endgroup\$ – Isiah Meadows May 30 '14 at 4:38
  • \$\begingroup\$ @impinball, Sorry? \$\endgroup\$ – unclemeat May 30 '14 at 4:59
  • \$\begingroup\$ Just pointing out a little bug. If you specify DOS, it isn't, but it'll break pretty easily in Windows (ex. in Program Files). No apologies needed. \$\endgroup\$ – Isiah Meadows May 30 '14 at 5:26
  • \$\begingroup\$ My bad...it's okay. I misremembered that quotes aren't needed in this case because they are automatically included in this. \$\endgroup\$ – Isiah Meadows May 30 '14 at 5:28
  • \$\begingroup\$ All good. Adding tilde removes surrounding quotes. %~0. \$\endgroup\$ – unclemeat May 30 '14 at 5:32
9
\$\begingroup\$

Ruby, 14 characters

File.delete $0

$0 is a special global variable that contains the name of the currently running script.

\$\endgroup\$
  • \$\begingroup\$ I'm pretty sure you can drop the space. \$\endgroup\$ – Darren Stone Jan 25 '14 at 8:26
  • \$\begingroup\$ This is why I made my version of the question much more strict. Interpreted languages can do it with no problem. The idea is that a natively running application can't just pull the rug out from underneath itself \$\endgroup\$ – Cruncher Jan 27 '14 at 14:22
6
\$\begingroup\$

BASIC-80 / BASICA / GW-BASIC / IBM BASIC / Commodore 64 BASIC / Vintage BASIC / Commodore LCD BASIC / Atari BASIC *

5 bytes

1 NEW

Well, that's about as simple as it gets. NEW creates a new program, so putting this anywhere in your program will delete it.

Proof of concept on IBM BASIC (putting NEW on line 40 for clarity):

* Yeah, I listed every old BASIC version I've tested this in (or pretty much ever used)

\$\endgroup\$
  • 2
    \$\begingroup\$ +1, There is something rather poetic about this. Existential even. \$\endgroup\$ – Wossname May 30 '17 at 20:27
5
\$\begingroup\$

PHP, 17 characters

unlink(__FILE__);
\$\endgroup\$
  • 5
    \$\begingroup\$ I'm not sure why anyone would downvote this; it's perfectly valid. You may want to include an open tag, though <?unlink(__FILE__); \$\endgroup\$ – primo Jan 31 '14 at 10:53
  • 3
    \$\begingroup\$ This PHP program prints unlink(__FILE__);. It does not delete the file. \$\endgroup\$ – rightfold May 8 '17 at 23:06
3
\$\begingroup\$

PowerShell (32)

del $MyInvocation.MyCommand.Name
\$\endgroup\$
3
\$\begingroup\$

Perl 5 (8 characters)

unlink$0

$0 is the script name, and unlink removes it. Normally, you'd at least add a space in between for readability.

\$\endgroup\$
3
\$\begingroup\$

C# (112)

Process.Start("cmd.exe","/C choice /C Y /N /D Y /T 3 & Del " + Application.ExecutablePath);
Application.Exit();

Shamelessly stolen from http://www.codeproject.com/Articles/31454/How-To-Make-Your-Application-Delete-Itself-Immedia

\$\endgroup\$
3
\$\begingroup\$

k (8)

~-1!.z.f

Or the q equivalent for 14:

hdel hsym .z.f
\$\endgroup\$
3
\$\begingroup\$

Python, 18

open(__file__,'w')

Opens itsself in write-only mode, erasing itself.

\$\endgroup\$
  • 7
    \$\begingroup\$ This truncates the file. It does not delete it. \$\endgroup\$ – rightfold May 8 '17 at 23:07
2
\$\begingroup\$

R, 26 characters

unlink(sys.frame(1)$ofile)
\$\endgroup\$
1
\$\begingroup\$

Node.js - 22 chars

fs.unlink(__filename);
\$\endgroup\$
1
\$\begingroup\$

Julia, 13 bytes

rm(@__FILE__)

Simple. But longer. :P

\$\endgroup\$
1
\$\begingroup\$

Vitsy + bash, 8 bytes

iG' mr',

iG       Get the name of the use declaration at the top item (-1) (returns the current one)
  ' mr'  Concatenate "rm " to the front of it.
       , Execute via shell.
\$\endgroup\$
1
\$\begingroup\$

Mathematica, 29 bytes

DeleteFile@NotebookFileName[]
\$\endgroup\$
1
\$\begingroup\$

Lua, 17 bytes

os.remove(arg[0])

This actually only works if you run the program by typing out the full filepath, i.e.
lua ~/Scripts/removeself.lua would work, but lua removeself.lua would not, assuming a filename of removeself.lua and a current working directory of ~/Scripts.

As far as I know, there's no way to find the actual filepath of a script, just the arguments passed to it. I do know about debug.getinfo(1).source, but in my testing that game exactly the same results as arg[0]. If anyone knows of a way to find the filepath, please let me know.

\$\endgroup\$
1
\$\begingroup\$

Nodejs, 32 bytes

require("fs").unlink(__filename)
\$\endgroup\$
1
\$\begingroup\$

JS, 37 bytes

document.documentElement.innerHTML=""

Does this count? It kills javascript on the page

\$\endgroup\$
  • \$\begingroup\$ You don't need the spaces around the = \$\endgroup\$ – programmer5000 May 10 '17 at 13:23
1
\$\begingroup\$

tcl, 18

file delete $argv0

demoATTENTION: It is only a one time demo! Only the first time it will be runnable. Please reserve it to the question's original poster. To try: press the Execute button. The green area will not report any error. After that, any subsequent clicks on the Execute button will result on a not found error!

\$\endgroup\$
1
\$\begingroup\$

C, 32 bytes

main(c,v)char**v;{remove(v[0]);}
\$\endgroup\$
  • 1
    \$\begingroup\$ Missing }s are allowed in C? Wow! \$\endgroup\$ – CalculatorFeline Jun 25 '17 at 14:57
  • \$\begingroup\$ Nope, just a typo! My character count included the closing } but I apparently copy/pasted the snippet incorrectly. Thanks for pointing it out! \$\endgroup\$ – Josh Jun 27 '17 at 15:59
  • \$\begingroup\$ Well, since the anonymous downvoter doesn't feel like leaving a comment or retracting their vote, I have deleted my answer, which I based off of yours. You can golf this down to 29 bytes: main(c,v)int**v;{remove(*v);} \$\endgroup\$ – MD XF Jul 22 '17 at 19:46
  • \$\begingroup\$ @MDXF I am not sure if changing char** to int** is technically valid. I'm worried about potential alignment issues. \$\endgroup\$ – Josh Jul 25 '17 at 17:42
  • 1
    \$\begingroup\$ @Josh Well; it works... so I don't see why not. Code golf is all about using horrible methods that aren't technically valid. \$\endgroup\$ – MD XF Jul 25 '17 at 17:42
1
\$\begingroup\$

JavaScript (ES6), 13 bytes

f=_=>delete f

Try It

Logs the source of f, calls f (which deletes f), tries to log f again but throws an error because f is now undefined.

f=_=>delete f
console.log(f)
f()
console.log(f)

\$\endgroup\$
0
\$\begingroup\$

PowerShell v3 - 17

ri $PSCommandPath
\$\endgroup\$
0
\$\begingroup\$

Julia - 25 bytes

Write this into a file, and then include the file with include("<filename here>") and run f():

f()=rm(functionloc(f)[1])

This is, obviously, a function - I'm not sure how one would go about detecting the file name of a file being run directly (as an include statement without a function definition in it).

\$\endgroup\$
0
\$\begingroup\$

C++, 137 bytes.

Works most of the time on windows

#include <stdlib.h> 
#include <string>
int main(int, char*v[]){char c[99];sprintf(c,"start cmd /C del \"%s\"/Q", *v);return system(c);}

C, 90 bytes

void main(int n,char**v){char c[99];sprintf(c,"start cmd /C del \"%s\"/Q", *v);system(c);} 
\$\endgroup\$
  • \$\begingroup\$ Most of the time? \$\endgroup\$ – Addison Crump Mar 4 '16 at 8:27
  • 1
    \$\begingroup\$ You could save a lot of bytes by removing the unused #include <string>, removing extraneous spaces, and making this C, which lets you remove the int and return. Additionally, you need #include <stdio.h> to make this work, since that's where sprintf is declared. \$\endgroup\$ – Mego Mar 4 '16 at 8:29
0
\$\begingroup\$

MATLAB, 36 bytes

delete([mfilename('fullpath'),'.m'])

Save the above code as an m-file following Matlab's filename guidelines.

\$\endgroup\$
0
\$\begingroup\$

VBA, 69 67 Bytes

Subroutine that takes no input and deletes itself.

Requires that the code below be the first line in the active code pane, and that VBA has trusted access to the VBE Project model.

Sub a:Application.VBE.CodePanes(1).CodeModule.DeleteLines 1:End Sub

-2 bytes for replacing ActiveCodePane with with CodePane(1)

\$\endgroup\$
0
\$\begingroup\$

T-SQL, 28 bytes

CREATE PROC d AS DROP PROC d

It just drops/removes the procedure and the code when executing itself. Usage:

EXECUTE d
\$\endgroup\$
0
\$\begingroup\$

shortC, 10 bytes

Aremove(*@

How it works:

A           main function
 remove(    delete file specified in next string
        *@  program name

Equivalent C program:

int main(int argc, char **argv){remove(*argv);}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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