Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to delete yourself?



  • Hello, may I ask. I want the Qt program .exe to delete itself. Is there such an API? In addition, how to control the background color of QTextEdit text selection through the style sheet?


  • Moderators

    @Nan-Feng said in How to delete yourself?:

    I want the Qt program .exe to delete itself. Is there such an API?

    The program cannot delete itself, because the file handle will stay used as long as the program runs.

    Anyhow, what you can do is start a script, that will delete the .exe once your application exited. The Installer Framework uses a similar setup to update itself, see e.g. the "deferredRename" function in https://code.qt.io/cgit/installer-framework/installer-framework.git/tree/src/libs/installer/packagemanagercore_p.cpp .



  • @kkoehne said in How to delete yourself?:

    The program cannot delete itself, because the file handle will stay used as long as the program runs.

    That depends on the OS. It wouldn't work on Windows. Linux, however, allows to change files which are open by someone else. If you delete a file while it is open, the filename is removed from the folder listing and the file is actually deleted when the last file handle is closed. @Nan-Feng Please let us know which operating system you use to figure out if it is even possible.

    You can use QCoreApplication::applicationFilePath() to figure out the filename of your .exe. QFile has a static function to delete files. However, this would not work on Windows.

    One trick for Windows could be that you have a separate .exe which just deletes a file provided by a command line argument (maybe have it wait a few seconds or try multiple times in loop to delete the file). Put this .exe as resource into your .exe. When you want to delete yourself, put a copy of the deleter .exe into a temporary path and call is from your .exe (using QProcess) and quit your own .exe. The deleter .exe should then be able to delete the original .exe file. You cannot delete your deleter .exe. However, if you place it in the temporary folder, Windows will delete it eventually.



  • I hope nobody will mind now if I nominate this thread for the best ever title! ;-)
    It intrigued me when I first saw it....



  • @SimonSchroeder But would not (on Windows) suffice to QProcess a routine (detached) that would erase the .exe once it stopped? I do remember writing self updater once that operated on the same principle: main thread was downloading the zip, start detached process, then detached process was unpacking the zip overwriting the main exe and all updated files, then starting the exe back and terminating self. It was working on Win7 and Win10 (last time I checked the company that was using this software of mine was in 2019 and it was working then).


  • Lifetime Qt Champion

    @JonB Be careful if you delete something, you could delete yourself :-)



  • @artwaw
    Under Linux or Windows, if the process has terminated it should be deletable. If a parent fork-execs (I don't know if exec without fork would also allow this, suspect it would) a child and does not wait for it, the child should be able to delete the parent file once the parent has exited.



  • @JonB exactly my point. I was referring to Windows as this was the only case I covered in practice.


Log in to reply