DEV Community

Cover image for Building a Self-Destructing CLI Tool in Go πŸš€πŸ’£
Siddhesh Khandagale
Siddhesh Khandagale

Posted on

4 1 2 1

Building a Self-Destructing CLI Tool in Go πŸš€πŸ’£

Ever wondered what it feels like to build a program that deletes itself after execution? Sounds crazy, right? Well, today, we are going to do just that, build a Self-Destructing CLI Tool in Go!

This is a fun experiment, but it also introduces some cool file handling and process management techniques in Go. Let’s get started!

How It Works
The idea is simple:

  • The Go program executes normally.
  • Before exiting, it deletes its own binary file.
  • Once destroyed, the program is gone forever (unless you recompile it). 🀯

Writing the Self-Destructing Go Code

1. Initialize a Go Project
First, create a new project directory and initialize a Go module:

mkdir self_destruct && cd self_destruct
go mod init self_destruct
Enter fullscreen mode Exit fullscreen mode

Create a file named self_destruct.go and add the following code:

package main

import (
 "fmt"
 "os"
 "os/exec"
)

func main() {
 fmt.Println("πŸ’€ Running self-destructing program...")

 // Get the current executable path
 exePath, err := os.Executable()
 if err != nil {
  fmt.Println("Failed to get executable path:", err)
  return
 }

 // Print the file path before deletion
 fmt.Println("Deleting:", exePath)

 // Create a command to delete the file
 if runtime.GOOS == "windows" {
     cmd = exec.Command("cmd", "/C", "del", exePath)
 } else {
     cmd = exec.Command("sh", "-c", fmt.Sprintf("rm -f %s", exePath))
 }
 cmd.Start() // Start but don't wait to prevent blocking
}
Enter fullscreen mode Exit fullscreen mode

Let’s break down what this code does:

  • os.Executable() returns the absolute path of the currently running executable.
  • fmt.Println("Deleting:", exePath): Prints the file path before deletion so the user can see what’s happening.
  • exec.Command("cmd", "/C", "del", exePath) creates a command to delete the executable file.
  • .Start() runs the command asynchronously to ensure the program doesn't hang while deleting itself.

Running the Program

Compile the Program : go build .
Run the Program : ./self_destruct

The binary file deletes itself after execution! Try running it again you’ll see it’s gone.

While this was a fun experiment, there are some real-world applications:

  • Security & Privacy β€” Temporary scripts that auto-delete after execution.
  • Cleanup Mechanisms β€” Self-cleaning build artifacts.
  • One-Time Setup Scripts β€” Ensure scripts don’t run twice.

This was a crazy but entertaining experiment with Go’s file handling and process execution.

Would you ever use this in real life? Or have any other wacky Go ideas? Let me know! πŸš€πŸ”₯

Sentry image

Make it make sense

Only the context you need to fix your broken code with Sentry.

Start debugging β†’

Top comments (0)

Image of PulumiUP 2025

Let's talk about the current state of cloud and IaC, platform engineering, and security.

Dive into the stories and experiences of innovators and experts, from Startup Founders to Industry Leaders at PulumiUP 2025.

Register Now

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay