How to create an uninstaller that uninstalls itself?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Gamemap

I want to write a program that first uninstalls another program and then deletes itself with its parent folder.

Is this possible? Is it only possible to transfer it to the %TEMP% folder?

Why wouldn’t it be possible? But I’m not sure why this is a Godot-specific question, especially since you can bundle a Godot game as a single executable that doesn’t actually need any kind of installation besides dropping it somewhere into the filesystem. Uninstalling the game then consists of deleting the file.

archeron | 2021-03-26 08:07

I want a programm that deletes my game and then itself, but this code won’t work.
Directory.remove(str(OS.get_executable_path().get_base_dir()))
(deleting the DeleteProgramm)

Gamemap | 2021-03-26 09:18

What error do you get from Directory.remove? And what version of Godot are you running it on? This is important because the behaviour of Directory.remove was changed several times in 3.2, and depending on which version you use, it works a bit differently.

archeron | 2021-03-26 12:46

Godot Version: 3.2.4 RC3

Error: 1 (Generic error)

I did not know that the folder must be empty. Unfortunately I can only delete the pck, not the exe file of my delete program (which is currently running).

Gamemap | 2021-03-26 17:04

:bust_in_silhouette: Reply From: archeron

If you use

Directory.remove(str(OS.get_executable_path().get_base_dir()))

You’re trying to delete a directory which isn’t empty, because it contains the executable. That doesn’t work. You need to delete the executable file first, and then the directory.

In addition to that, I’m not sure whether you need to pass Directory.remove a prefix that tells it to use your actual file system root, because Directory.remove might work relative to your res:// folder by default. Probably isn’t a problem, though, since OS.get_executable_path will hopefully return an absolute path that’s recognized by Directory.remove as such.

Note that depending on which version of Godot you’re using, the semantics of Directory.remove work out slightly differently.