Deletes the currently running executable.
Use this when a process should remove its own executable file after it has
finished running, such as a one-shot bootstrapper or an uninstall helper.
This is useful when the executable should clean up its on-disk image as part
of its own shutdown flow.
Returns
Returns Ok(()) after the file has been unlinked on Unix platforms, or
after the deletion helper has been scheduled successfully on Windows.
The function does not report whether the current process has already exited;
it only reports whether the delete operation or delayed delete setup
succeeded.
Platform behavior
- Linux and macOS unlink the file immediately.
- Windows launches a detached helper script that waits for the process to
exit, then removes the executable file.
Notes
- On Unix hosts, unlinking removes the directory entry immediately even
though the current process may continue running until it exits.
- On Windows, a successful return means deletion was only scheduled
successfully. Callers should exit soon after Ok(()) so the helper can
remove the file.
Errors
Returns Err(ReplaceSelfError) when:
- ExecutablePathUnavailable: the runtime cannot determine the current
executable path
- UnsupportedPlatform: the host platform is unsupported by the native shim
- NativeFailure: the operating system rejects the deletion request
Example
match @replace_self.delete_self() {
Ok(()) => ()
Err(error) => println(error)
}