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>
-
\$\begingroup\$ possible duplicate of Write a program that deletes itself \$\endgroup\$John Dvorak– John Dvorak2014年01月25日 03:39:04 +00:00Commented Jan 25, 2014 at 3:39
-
2\$\begingroup\$ @JanDvorak That question was stricter, requiring a .exe file. This is more lax. \$\endgroup\$EMBLEM– EMBLEM2014年01月25日 03:39:50 +00:00Commented Jan 25, 2014 at 3:39
-
\$\begingroup\$ Hmm... what if I vote to close in the opposite direction, then? \$\endgroup\$John Dvorak– John Dvorak2014年01月25日 03:41:14 +00:00Commented Jan 25, 2014 at 3:41
-
5\$\begingroup\$ code-golf, not popularity-contest? Aww. \$\endgroup\$Justin– Justin2014年01月25日 04:19:08 +00:00Commented 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– EMBLEM2014年01月25日 04:24:27 +00:00Commented Jan 25, 2014 at 4:24
30 Answers 30
Bash script (7 characters, 7 bytes)
rm "0ドル"
-
1\$\begingroup\$ Doesn't work if the script file name contains spaces. \$\endgroup\$user80551– user805512014年01月25日 03:46:16 +00:00Commented 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– anthony.c2014年01月25日 03:50:08 +00:00Commented Jan 25, 2014 at 3:50
-
\$\begingroup\$ Does this work if the file name contains double quotes? \$\endgroup\$John Dvorak– John Dvorak2014年01月25日 07:28:11 +00:00Commented 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– Runium2014年01月26日 04:37:21 +00:00Commented 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– MD XF2017年05月30日 16:33:10 +00:00Commented May 30, 2017 at 16:33
Unix? (9):
#!/bin/rm
A classic. Uses rm
as interpreter, for instant self-deletion. Not the very shortest though.
-
5\$\begingroup\$ Or if you want to be cheaty: a 0-byte
rm
program. \$\endgroup\$user344– user3442014年07月02日 18:07:39 +00:00Commented Jul 2, 2014 at 18:07 -
2\$\begingroup\$ The
#!
line isn't counted in score. \$\endgroup\$Pavel– Pavel2017年05月07日 03:10:24 +00:00Commented 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– user621312017年06月16日 06:06:16 +00:00Commented 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– user621312017年06月16日 06:06:54 +00:00Commented Jun 16, 2017 at 6:06 -
\$\begingroup\$ @Pavel Hmm... 0 bytes? \$\endgroup\$ɐɔıʇǝɥʇuʎs– ɐɔıʇǝɥʇuʎs2017年09月11日 11:46:13 +00:00Commented Sep 11, 2017 at 11:46
Batch - 6 bytes
Del %0
Pretty simple. Note: Works even if the name contains spaces.
-
\$\begingroup\$ Only if the file name doesn't have spaces. \$\endgroup\$Claudia– Claudia2014年05月30日 04:38:10 +00:00Commented May 30, 2014 at 4:38
-
\$\begingroup\$ @impinball, Sorry? \$\endgroup\$unclemeat– unclemeat2014年05月30日 04:59:50 +00:00Commented 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– Claudia2014年05月30日 05:26:09 +00:00Commented 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– Claudia2014年05月30日 05:28:42 +00:00Commented May 30, 2014 at 5:28
-
\$\begingroup\$ All good. Adding tilde removes surrounding quotes.
%~0
. \$\endgroup\$unclemeat– unclemeat2014年05月30日 05:32:43 +00:00Commented May 30, 2014 at 5:32
Ruby, 14 characters
File.delete 0ドル
0ドル
is a special global variable that contains the name of the currently running script.
-
\$\begingroup\$ I'm pretty sure you can drop the space. \$\endgroup\$Darren Stone– Darren Stone2014年01月25日 08:26:29 +00:00Commented 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– Cruncher2014年01月27日 14:22:49 +00:00Commented Jan 27, 2014 at 14:22
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)
-
2\$\begingroup\$ +1, There is something rather poetic about this. Existential even. \$\endgroup\$Wossname– Wossname2017年05月30日 20:27:01 +00:00Commented May 30, 2017 at 20:27
PHP, 17 characters
unlink(__FILE__);
-
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– primo2014年01月31日 10:53:06 +00:00Commented Jan 31, 2014 at 10:53 -
5\$\begingroup\$ This PHP program prints
unlink(__FILE__);
. It does not delete the file. \$\endgroup\$user11779– user117792017年05月08日 23:06:25 +00:00Commented 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– Darren H2019年08月01日 10:01:57 +00:00Commented Aug 1, 2019 at 10:01
PowerShell (32)
del $MyInvocation.MyCommand.Name
Perl 5 (8 characters)
unlink0ドル
0ドル
is the script name, and unlink
removes it. Normally, you'd at least add a space in between for readability.
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
R, 26 characters
unlink(sys.frame(1)$ofile)
k (8)
~-1!.z.f
Or the q equivalent for 14:
hdel hsym .z.f
Python, 18
open(__file__,'w')
Opens itsself in write-only mode, erasing itself.
-
11\$\begingroup\$ This truncates the file. It does not delete it. \$\endgroup\$user11779– user117792017年05月08日 23:07:23 +00:00Commented May 8, 2017 at 23:07
Node.js - 22 chars
fs.unlink(__filename);
Julia, 13 bytes
rm(@__FILE__)
Simple. But longer. :P
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.
Mathematica, 29 bytes
DeleteFile@NotebookFileName[]
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.
JS, 37 bytes
document.documentElement.innerHTML=""
Does this count? It kills javascript on the page
-
\$\begingroup\$ You don't need the spaces around the
=
\$\endgroup\$user58826– user588262017年05月10日 13:23:56 +00:00Commented May 10, 2017 at 13:23
tcl, 18
file delete $argv0
demo — ATTENTION: 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!
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);}
C, 32 bytes
main(c,v)char**v;{remove(v[0]);}
-
2\$\begingroup\$ Missing
}
s are allowed in C? Wow! \$\endgroup\$CalculatorFeline– CalculatorFeline2017年06月25日 14:57:12 +00:00Commented 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– Josh2017年06月27日 15:59:55 +00:00Commented 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– MD XF2017年07月22日 19:46:53 +00:00Commented Jul 22, 2017 at 19:46 -
\$\begingroup\$ @MDXF I am not sure if changing
char**
toint**
is technically valid. I'm worried about potential alignment issues. \$\endgroup\$Josh– Josh2017年07月25日 17:42:18 +00:00Commented 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– MD XF2017年07月25日 17:42:50 +00:00Commented Jul 25, 2017 at 17:42
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)
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.
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
PowerShell v3 - 17
ri $PSCommandPath
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).
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);}
-
\$\begingroup\$ Most of the time? \$\endgroup\$Addison Crump– Addison Crump2016年03月04日 08:27:04 +00:00Commented 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 theint
andreturn
. Additionally, you need#include <stdio.h>
to make this work, since that's wheresprintf
is declared. \$\endgroup\$user45941– user459412016年03月04日 08:29:04 +00:00Commented Mar 4, 2016 at 8:29
MATLAB, 36 bytes
delete([mfilename('fullpath'),'.m'])
Save the above code as an m-file following Matlab's filename guidelines.
Nodejs, 32 bytes
require("fs").unlink(__filename)
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