254
\$\begingroup\$

Goal

The goal of this challenge is to write code that will execute once and only once. This means basically that it damages the program, script, or environment in some way. If rebooting the system allows the code to run again that is permitted.

Scoring

Number of votes. All assumptions must be clearly listed. Any answers just initiating a reboot or halt will be disqualified.

Additional Rules because Greg Hewgill is a demi-god

No root access is permitted.

End Date

The contest will close on May 31, 2014.

Edit

This contest has been changed to popularity contest.

Wheat Wizard
103k23 gold badges299 silver badges696 bronze badges
asked May 28, 2014 at 3:40
\$\endgroup\$
17
  • 8
    \$\begingroup\$ possible duplicate of A program that deletes itself \$\endgroup\$ Commented May 28, 2014 at 7:04
  • 7
    \$\begingroup\$ @PeterTaylor a possible Solution would be the a self delete but as the result are showing it isn't the only one. \$\endgroup\$ Commented May 28, 2014 at 7:21
  • 119
    \$\begingroup\$ To a lot of people calling vi in a single terminal environment has this effect, no escape from it unless you reboot :) Just a joke here. \$\endgroup\$ Commented May 28, 2014 at 20:41
  • 25
    \$\begingroup\$ echo "If you try to execute me again, it means you are an idiot."; <-- Nobody will execute more than once :P \$\endgroup\$ Commented Jun 5, 2014 at 12:45
  • 36
    \$\begingroup\$ Would missile-related software have qualified? ;) \$\endgroup\$ Commented Jun 8, 2014 at 4:28

83 Answers 83

7
\$\begingroup\$

Here's a couple. Unlike many, these are not destructive, just creative and unorthodox.

Bash

#!/usr/bin/env bash
read <<< ''
printf "$REPLY" > "0ドル"

This one's pretty simple. The variable $REPLY is created implicitly by read, but filled with an empty herestring. That empty string is then printf'ed into the current script. It is a rather obfuscated equivalent of the following:

#!/usr/bin/env bash
cat <<< '' > "0ドル" # probably a lot more portable

Windows Batch

copy con %0 < nul

The second one basically copies the console input, read from nul, into the current file.

answered May 30, 2014 at 5:22
\$\endgroup\$
6
  • \$\begingroup\$ Posted after this became a popularity contest \$\endgroup\$ Commented May 30, 2014 at 5:31
  • 1
    \$\begingroup\$ The first one won't work without root permissions. Why are you trying to replace the null device? When run as root, this would actually make the system behave erratically. You are also overwriting the null while writing into it :) Why not just move it anywhere else? Such as mv "0ドル" "${0}x" or something? It should do the trick. \$\endgroup\$ Commented May 30, 2014 at 9:14
  • \$\begingroup\$ Does the file still exist afterwards? \$\endgroup\$ Commented May 30, 2014 at 16:34
  • \$\begingroup\$ I now completely changed the Bash example. \$\endgroup\$ Commented May 30, 2014 at 18:03
  • \$\begingroup\$ How are these not destructive? \$\endgroup\$ Commented Jun 5, 2014 at 23:05
7
\$\begingroup\$

Minecraft Command Block

setblock ~ ~ ~ air

When you run this in a command block, it destroys itself.

answered Jun 3, 2021 at 20:56
\$\endgroup\$
5
\$\begingroup\$

SQL

create proc P as drop proc P

SQL Fiddle

answered Jun 2, 2014 at 9:53
\$\endgroup\$
5
\$\begingroup\$

x86 Machine Code (5 bytes)

HEX:

EA0000FFFF

ASM:

JMP FFFF:0000

You can try with debug.exe:

C:\>debug
-a 100
0AE7:0100 jmp ffff:0000
0AE7:0105
-g=100

Explanation: BIOSis always at FFFF:0000 in memory. So this sequence boots the computer, if this is run in a protected command prompt in Windows it makes that process unresponsive.

answered Jun 2, 2014 at 9:43
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Would you mind adding an explanation for those not that familiar with machine code? \$\endgroup\$ Commented Jun 2, 2014 at 9:44
5
\$\begingroup\$

ARM Linux full standalone ELF, (削除) 50 (削除ここまで) (削除) 46 (削除ここまで) 45 bytes

7f 45 4c 46 01 00 00 00 00 00 00 00 00 00 01 00
02 00 28 00 21 00 01 00 21 00 01 00 04 00 00 00
01 98 0a 27 00 df 01 27 00 df 20 00 01

Adapted version of the code used in A Whirlwind Tutorial on Creating Really Teensy ELF Executables for Linux.

Assembler source (with raw encoded ELF header and comments, most of which are the same as in the article)

@ Adaptation of
@ https://www.muppetlabs.com/~breadbox/software/tiny/teensy.html
@ for an ARM binary which deletes itself. XD
@ $ arm-none-eabi-as -march=armv4t -mthumb runonce-arm.s -o runonce-arm.o
@ $ arm-none-eabi-ld -Ttext 0x00010000 runonce-arm.o -o runonce-arm.elf
@ $ arm-none-eabi-objcopy -O binary runonce-arm.elf runonce-arm
 .text @ A lie.
 .arch armv4t @ Set architecture version
 .thumb @ Needs to be Thumb to fit. ARM is too thicc.
 .syntax unified
 .org 0 @ Start at offset 0
Elf32_Ehdr:
 .byte 0x7F,'E','L','F' @ e_ident
Elf32_Phdr:
 .word 1 @ p_type
 .word 0 @ p_offset
 .word 0x00010000 @ same as -Ttext @ p_vaddr
 .short 2 @ ET_EXEC @ e_type @ p_paddr
 .short 40 @ EM_ARM @ e_machine
 .word _start @ e_version @ p_filesz
 .word _start @ e_entry @ p_memsz
 .word 4 @ Elf32_Phdr @ e_phoff @ p_flags
 @ Here is some space to put our code in.
 .thumb_func
 .globl _start
 _start:
 @ r0 = argv[0] from _start state
 ldr r0, [sp, #4] @ e_shoff @ p_align
 @ unlink(argv[0])
 movs r7, #0x0a @ unlink
 svc #0 @ syscall @ e_flags
 @ exit(dontcare)
 movs r7, #0x01 @ exit
 svc #0 @ syscall @ e_ehsize
 @ unreachable
 @ finish the header
 .short 0x20 @ e_phentsize
 .byte 1 @ e_phnum
 @ Truncated header because for some reason, Linux accepts this.

Basically, I took this program:

#include <unistd.h>
int main(int argc, char *argv[])
{
 unlink(argv[0]);
}

Or if it is easier to read,

main(c,v)int*v;{unlink(*v);}

And squeezed it into an ELF header using the method in that article, so it is 45 bytes with no dependencies on libc.

Works every time except when it is called from $PATH.

answered Dec 26, 2020 at 2:10
\$\endgroup\$
5
\$\begingroup\$

Error safe termination (Python 2/3)

from contextlib import contextmanager
@contextmanager
def suicide():
 try: yield
 finally:
 open(__file__ , 'w')
 open(__file__+'c', 'w')
#-------------------------------------
# Error safe code ;)
#-------------------------------------
with suicide():
 print("Goodbye cruel world!")
 jump_off_building_______crash
answered Jun 3, 2014 at 19:26
\$\endgroup\$
1
  • \$\begingroup\$ I like how this "suicides". \$\endgroup\$ Commented Jul 9, 2017 at 23:39
4
\$\begingroup\$

Ruby, (削除) 15 (削除ここまで) 14

Put this line to a file (del.rb):

File.delete 0ドル

then run it (self-destructive) : ruby del.rb del.rb

answered May 28, 2014 at 6:44
\$\endgroup\$
1
  • 1
    \$\begingroup\$ you can save 1 character by using :File.delete 0ドル \$\endgroup\$ Commented May 28, 2014 at 7:45
4
\$\begingroup\$

PHP - 23

<?=fopen(__FILE__,'w');
answered May 30, 2014 at 12:46
\$\endgroup\$
2
  • \$\begingroup\$ I wonder if it is possible to drop the semicolon here. I know it's possible with short tags like this: <?= $var ?>. \$\endgroup\$ Commented Jun 1, 2014 at 12:09
  • \$\begingroup\$ @naiply: No. ?> implies semicolon (you can even get a syntax error mentioning ;), but end of document doesn't. \$\endgroup\$ Commented Jun 1, 2014 at 14:33
4
\$\begingroup\$

Coffescript

_=->_=1

compiles to:

var _;
_ = function() {
 return _ = 1;
};
answered Jun 4, 2014 at 17:49
\$\endgroup\$
4
\$\begingroup\$

ESMin (noncompetitive)

ɟ`html"Ĭ)

Try it here (Firefox only).

Translates to $('html').remove() in Javascript.

answered Feb 6, 2016 at 23:51
\$\endgroup\$
2
  • \$\begingroup\$ What if my HTML page has no <html> element? \$\endgroup\$ Commented Jun 7, 2016 at 20:18
  • 1
    \$\begingroup\$ ESMin runs in the online interpreter, so that shouldn't be a problem \$\endgroup\$ Commented Jun 8, 2016 at 16:35
4
\$\begingroup\$

R, 7 Bytes

`=`=`$`

The code makes the "=" (used for assigning) become the "$" (used for subseting). After that it gives error.

answered Apr 12, 2016 at 15:13
\$\endgroup\$
2
  • \$\begingroup\$ I don't think this is valid. If I put it in a file I can run it as many times as I want, right? \$\endgroup\$ Commented Jul 9, 2017 at 23:41
  • 1
    \$\begingroup\$ @EsolangingFruit, no on repeat execution this throws the error Error in `=` = `$` : object of type 'special' is not subsettable as validated in R version 3.4.3 (2017年11月30日) \$\endgroup\$ Commented Mar 13, 2018 at 5:34
3
\$\begingroup\$

Bash: 4 chars

rm a

Put this in a file named a and run it on your Linux machine.

answered May 28, 2014 at 6:37
\$\endgroup\$
7
  • 13
    \$\begingroup\$ rm * will work for every filename \$\endgroup\$ Commented May 28, 2014 at 12:24
  • 3
    \$\begingroup\$ @AntonioRagagnin It's better to stick to non-destructive scripts \$\endgroup\$ Commented May 28, 2014 at 12:52
  • 1
    \$\begingroup\$ rm 0ドル will also work for all filenames, at the cost of one character. \$\endgroup\$ Commented May 31, 2014 at 19:05
  • 7
    \$\begingroup\$ @AntonioRagagnin So will rm -rf / --no-preserve-root, but better ;) \$\endgroup\$ Commented May 31, 2014 at 21:49
  • 6
    \$\begingroup\$ @fNek No, it won't. Only for those without a whitespace in their name. \$\endgroup\$ Commented May 31, 2014 at 21:57
3
\$\begingroup\$

Plain TeX - 31 (24) chars

\openout1=\jobname\write1{}\bye

If you save it as file a.tex then:

\openout1=a\write1{}\bye
answered Jun 3, 2014 at 12:26
\$\endgroup\$
3
\$\begingroup\$

Bash

This deletes the file and makes sure that you can't recover it. So it definitely can't execute more than once.

shred "0ドル"

shred is a program to securely delete files by overwriting them. It's in coreutils.

answered Jun 3, 2014 at 12:52
\$\endgroup\$
3
\$\begingroup\$

AutoHotKey, 39 bytes

FileDelete %A_ScriptDir%\%A_ScriptName%

(Yes, I'm aware this is an ancient thread)

answered Mar 30, 2016 at 18:39
\$\endgroup\$
3
\$\begingroup\$

Dyalog APL (non-competing because it is too late)

f←⎕EX'f'∘⊣

f can only be called once. It needs an argument, which is ignored:

 f←⎕EX'f'∘⊣
 f 'dummy argument'
 f 'dummy argument'
VALUE ERROR
 f'dummy argument'
 ∧

It is a so-called "atop", which is a train of two functions. The left one is applied on the result of the right one.

The right function is 'f'∘⊣ which replaces any given argument with the string 'f'.

The left function is ⎕EX, which EXpunges the object named in its argument.

The first time around, 'f'∘⊣ returns 'f', which causes ⎕EX to erase f. The second time around, calling f results in an error because f does not exist.


Here is one that can be tried online:

f←{f∘← ̈}

After defining the function, the first time that it is called (with a dummy argument), e.g. f 4, the function ignores the argument, and instead redefines itself to be an operator (higher-order function). Now, operators need at least a left operand so when it is called again, the call will fail with a syntax error.

answered Feb 24, 2016 at 1:39
\$\endgroup\$
2
  • \$\begingroup\$ Who said "too late" means "non-competitive"? \$\endgroup\$ Commented Apr 23, 2016 at 7:54
  • \$\begingroup\$ @ΈρικΚωνσταντόπουλος Mama Fun Roll. But you did make me take a look at what I had written, and I found a typo. Thanks! \$\endgroup\$ Commented Apr 25, 2016 at 17:27
3
\$\begingroup\$

Linux, 8 bytes

killall5

Similar to this answer, but this works on pretty much any Linux since System V and you don't need to be root. Don't believe me? Try it out on your Linux system! You'll want to save any work first...

I actually found this by accident when I was 11 messing around with Linux for the first time.

Links: killall5 manpage, recording

answered May 22, 2017 at 16:39
\$\endgroup\$
3
\$\begingroup\$

Commodore 64 BASIC, 7 bytes

POKE 1,3

I've had a lot of fun in various BASIC dialects POKE-ing around in the first few memory storage locations. Here's the effect of this one:

poke 1,3

Try it online! You'll have to type it in yourself.

Some other POKEs:

  • POKE 1,1 will freeze the system.
  • POKE 1,2 will clear the screen.
  • POKE 1,4 completely kills the system - the power button won't even work.
  • POKE 1,5 through POKE 1,9 do the same as POKE 1,4, but with varying levels of destruction.
answered May 30, 2017 at 15:26
\$\endgroup\$
3
\$\begingroup\$

TIS-100, 6 bytes

@0
HCF

The HCF (or halt and catch fire) command instantly crashes the TIS-100. The only way to run it again is to start it up again.

answered Jul 10, 2017 at 17:14
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Invalid, I'm afraid. From the challenge: "Any answers just initiating a reboot or halt will be disqualified." \$\endgroup\$ Commented Nov 15, 2017 at 16:22
3
\$\begingroup\$

R16K1S60 Assembly

mov [ax], ax

Simply overrides itself with with the contents of AX. the mov instruction is only one word large, so it deletes itself. Depends on AX being 0 (most releases guarentee this)

Safe version

mov [0], ax

Depends on ax not being this specific instruction

answered Mar 13, 2018 at 6:30
\$\endgroup\$
6
  • \$\begingroup\$ Woah, I never thought I'd see this language here. \$\endgroup\$ Commented Mar 13, 2018 at 19:45
  • \$\begingroup\$ @12Me21 Never say that around PPCG. Ever. We use everything ;) \$\endgroup\$ Commented Mar 16, 2018 at 6:53
  • \$\begingroup\$ I wonder if LBPHacker will ever release the new computer he made. I'm guessing he's either really busy or he's just started over. Anyway, it might be better to use mov [0], ax since it doesn't rely on ax being 0. \$\endgroup\$ Commented Mar 16, 2018 at 15:21
  • \$\begingroup\$ @12Me21 I can confirm he hasn't started over. He's just busy. Also, join #powder on freenode. \$\endgroup\$ Commented Mar 16, 2018 at 20:01
  • \$\begingroup\$ I believe the instruction pointer actually points to the next memory cell, not the current one. \$\endgroup\$ Commented Mar 16, 2018 at 20:17
3
\$\begingroup\$

Wolfram Language (Mathematica): 37 Characters

Export[#,""]&/@FileNames[All,"C:/",∞]

Never tested this fully, but it should overwrite every single file on a Windows computer, including the Mathematica installation and the file the program is saved in by replacing all data with an empty string. Actually useful for quickly clearing out a folder, but most definitely not something I want to run.

answered Jan 7, 2022 at 21:10
\$\endgroup\$
2
\$\begingroup\$

Haskell - 80 bytes

Pre base-4.6.0.0 : May not work on Windows. This depends on how the program is invoked.

import System.Environment
import System.Directory
main=getProgName>>=removeFile

Post base-4.6.0.0 : A bit longer but always works.

import System.Environment
import System.Directory
main=getExecutablePath>>=removeFile
answered May 28, 2014 at 20:52
\$\endgroup\$
1
  • \$\begingroup\$ It's now a popularity contest. \$\endgroup\$ Commented May 30, 2014 at 5:41
2
\$\begingroup\$

JAVASCRIPT ((削除) 35 (削除ここまで) 26 bytes when minified, in case that matters.) I know this is a late entry, but I had a situation where I considered being able to kill a function and decided it'd make a good entry here. (Still not sure if I'll actually use it.)

funny = function(){
 alert(0);
 window.funny = '';
};
funny();
funny();

minified

a=function(){alert(0);window.a=""};

Alert isn't really needed

a=function(){window.a=""};
answered Jun 5, 2014 at 15:23
\$\endgroup\$
1
  • \$\begingroup\$ Just do function a(){a=0} \$\endgroup\$ Commented Mar 30, 2016 at 22:05
2
\$\begingroup\$

Python 2.7

import inspect
import os
for x in range(0, 2):
 os.remove(inspect.stack()[0][1])
 print "am I dead yet?"

It only runs 1/2 of a time.

answered Jun 6, 2014 at 3:33
\$\endgroup\$
2
\$\begingroup\$

Shell script (Linux Bash); 4 characters

rm *

Warning ! Don’t try this at home.

This shell script can be run only once. When run, this shell script will self-destruct.

NoOneIsHere
2,2171 gold badge23 silver badges48 bronze badges
answered Jun 3, 2014 at 15:21
\$\endgroup\$
2
  • 1
    \$\begingroup\$ For non-bash users: * is a glob, which expands to a list of all the files in the current directory. so it will delete everything in the folder the script is in as well as itself \$\endgroup\$ Commented Jun 9, 2014 at 19:26
  • 3
    \$\begingroup\$ This implicitly assumes that the process' "current directory" matches the script directory. The "run once" property will fail in other cases. Example: say script is in ~/a, current directory is ~/b. The script can be run with ../a/thescript and it will only try to delete in ~/b which won't delete the script (it's not in that directory). b may even be already empty before the script runs. \$\endgroup\$ Commented Jan 9, 2016 at 6:31
2
\$\begingroup\$

C, 56 chars

x[9];main(c,v)int**v;{sprintf(x,"rm %s",*v);system(x);}

Run on a UNIX system. By convention, the first string param passed to a C program is the executable name. This program simply deletes the executable (but leaves the source, which you have to recompile to run it again).

answered May 29, 2017 at 18:37
\$\endgroup\$
2
  • \$\begingroup\$ 54 bytes \$\endgroup\$ Commented Apr 1, 2019 at 4:08
  • \$\begingroup\$ why not just unlink(*v) or remove(*v) ? \$\endgroup\$ Commented Aug 6, 2023 at 9:00
2
\$\begingroup\$

oldschool DOS


You don't even need a compiler or interpreter, just the prompt. Not even reboot will let it run again once it completes (note: do not try on something you don't want to sacrifice. OS likely won't boot either.)

deltree C:\
answered Mar 13, 2018 at 8:03
\$\endgroup\$
2
\$\begingroup\$

Windows batch, 4 bytes

.>%0

This will attempt to execute '.' as a command, while redirecting stdout to the original file, resulting in destruction of the original file.

answered Mar 13, 2018 at 21:25
\$\endgroup\$
2
\$\begingroup\$

Scratch, 49 bytes

when gf clicked
wait until<(n)<(1
change[n v]by(2

Assuming n=0, which is the initial state of a variable when it is created.

answered Jun 3, 2021 at 17:39
\$\endgroup\$
1
\$\begingroup\$

Python 24

Name the file 'q'

import os
os.remove("q")
answered May 28, 2014 at 16:19
\$\endgroup\$
1
  • 5
    \$\begingroup\$ try os.remove(__file__) instead. \$\endgroup\$ Commented Apr 23, 2016 at 7:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.