26
\$\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.

html,body{margin:0;padding:0;height:100%;overflow:hidden}
 <iframe src="https://xmikee1.github.io/ppcg-leaderboard/?id=19355" width="100%" height="100%" style="border:none;">Oops, your browser is too old to view this content! Please upgrade to a newer version of your browser that supports HTML5.</iframe>

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

30 Answers 30

24
\$\begingroup\$

Bash script (7 characters, 7 bytes)

rm "$0"
\$\endgroup\$
9
  • 1
    \$\begingroup\$ Doesn't work if the script file name contains spaces. \$\endgroup\$
    – user80551
    Commented Jan 25, 2014 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
    Commented Jan 25, 2014 at 3:50
  • \$\begingroup\$ Does this work if the file name contains double quotes? \$\endgroup\$ Commented Jan 25, 2014 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
    Commented Jan 26, 2014 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
    Commented May 30, 2017 at 16:33
19
\$\begingroup\$

Unix? (9):

#!/bin/rm

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

\$\endgroup\$
5
  • 5
    \$\begingroup\$ Or if you want to be cheaty: a 0-byte rm program. \$\endgroup\$
    – user344
    Commented Jul 2, 2014 at 18:07
  • 2
    \$\begingroup\$ The #! line isn't counted in score. \$\endgroup\$
    – Pavel
    Commented May 7, 2017 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
    Commented Jun 16, 2017 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
    Commented Jun 16, 2017 at 6:06
  • \$\begingroup\$ @Pavel Hmm... 0 bytes? \$\endgroup\$ Commented Sep 11, 2017 at 11:46
10
\$\begingroup\$

Batch - 6 bytes

Del %0

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

\$\endgroup\$
7
  • \$\begingroup\$ Only if the file name doesn't have spaces. \$\endgroup\$
    – Claudia
    Commented May 30, 2014 at 4:38
  • \$\begingroup\$ @impinball, Sorry? \$\endgroup\$
    – unclemeat
    Commented May 30, 2014 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\$
    – Claudia
    Commented May 30, 2014 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\$
    – Claudia
    Commented May 30, 2014 at 5:28
  • \$\begingroup\$ All good. Adding tilde removes surrounding quotes. %~0. \$\endgroup\$
    – unclemeat
    Commented May 30, 2014 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\$
2
  • \$\begingroup\$ I'm pretty sure you can drop the space. \$\endgroup\$ Commented Jan 25, 2014 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
    Commented Jan 27, 2014 at 14:22
7
\$\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\$
1
  • 2
    \$\begingroup\$ +1, There is something rather poetic about this. Existential even. \$\endgroup\$
    – Wossname
    Commented May 30, 2017 at 20:27
6
\$\begingroup\$

PHP, 17 characters

unlink(__FILE__);
\$\endgroup\$
3
  • 7
    \$\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
    Commented Jan 31, 2014 at 10:53
  • 5
    \$\begingroup\$ This PHP program prints unlink(__FILE__);. It does not delete the file. \$\endgroup\$
    – user11779
    Commented May 8, 2017 at 23:06
  • 1
    \$\begingroup\$ @rightfold it works as described if you include an opening tag, which I would have assumed to add \$\endgroup\$
    – Darren H
    Commented Aug 1, 2019 at 10:01
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\$

R, 26 characters

unlink(sys.frame(1)$ofile)
\$\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\$
1
  • 11
    \$\begingroup\$ This truncates the file. It does not delete it. \$\endgroup\$
    – user11779
    Commented May 8, 2017 at 23:07
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\$

JS, 37 bytes

document.documentElement.innerHTML=""

Does this count? It kills javascript on the page

\$\endgroup\$
1
  • \$\begingroup\$ You don't need the spaces around the = \$\endgroup\$
    – user58826
    Commented May 10, 2017 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\$

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\$
1
\$\begingroup\$

C, 32 bytes

main(c,v)char**v;{remove(v[0]);}
\$\endgroup\$
5
  • 2
    \$\begingroup\$ Missing }s are allowed in C? Wow! \$\endgroup\$ Commented Jun 25, 2017 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
    Commented Jun 27, 2017 at 15:59
  • 1
    \$\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
    Commented Jul 22, 2017 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
    Commented Jul 25, 2017 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
    Commented Jul 25, 2017 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\$
1
\$\begingroup\$

8086/8088 machine code, 1 byte

aa    stosb

Assumptions:

  • The registers CS, IP, ES, DI, and AL are all initially 0.
  • This code is placed at address 0.

The stosb instruction stores the byte in the AL register at the memory location pointed to by ES and DI, then increments or decrements DI. In this case, it stores the byte 0 at the memory address 0, which happens to be where the instruction itself is.

Granted, this program doesn't delete a file or anything. But if you manage to get your hands on an 8086 processor, poke this program into memory, and run it, it will in fact overwrite itself.

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

VBA, 69 62 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:Parent.VBE.CodePanes(1).CodeModule.DeleteLines 1:End Sub

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

-5 bytes for replacing Application with Parent

\$\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\$
2
  • \$\begingroup\$ Most of the time? \$\endgroup\$ Commented Mar 4, 2016 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\$
    – user45941
    Commented Mar 4, 2016 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\$

Nodejs, 32 bytes

require("fs").unlink(__filename)
\$\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\$

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.