11

I noticed this warning in the documentation for NSIS's RmDir method:

Warning: using RMDir /r $INSTDIR in the uninstaller is not safe. Though it is unlikely, the user might select to install to the Program Files folder and so this command will wipe out the entire Program Files folder, including other programs that has nothing to do with the uninstaller.

This scares me, since up until now I had not considered this possibility and I had that exact line in my script. But when I tested if this would happen by installing my program to a pre-existing location containing pre-existing files and then running my uninstaller with RmDir /r /REBOOTOK $INSTDIR in it, the existing files were left unharmed.

Is this an outdated warning? I'm using NSIS v. 2.46.

Thanks

Cuga
  • 17,668
  • 31
  • 111
  • 166

2 Answers2

7

RmDir /r will delete the whole directory tree if it can, so it is "unsafe". See http://nsis.sourceforge.net/Uninstall_only_installed_files for a way to only delete the files you install

Anders
  • 97,548
  • 12
  • 110
  • 164
  • 1
    What if I were to create a sub folder at $INSTDIR and put all my program data in there, recursively delete that sub dir, and then do a regular RmDir on the original $INSTDIR? – Cuga Jul 16 '10 at 12:46
  • 1
    Well, it would help, but the issue could still be there. Lets say your subfolder is named system32 and the user installs to %windir%, you will then delete %windir%\system32 – Anders Jul 16 '10 at 16:56
  • I think I'll risk it with creating a subdir inside INSTDIR named: ... I think this should work. – Cuga Jul 16 '10 at 17:59
  • The code example doesn't appear to remove the $INSTDIR. Should I just leave the directory there, or is there a safe way of doing this? – parsley72 Jun 15 '11 at 00:59
  • 3
    This method of uninstalling only installed files is too complicated. I know that's my opinion, and I really think there should be an easier way to do this. It's too prone to errors and takes too many lines of code. – Jason Sep 10 '18 at 16:29
  • https://nsis.sourceforge.io/Validating_$INSTDIR_before_uninstall "What if $INSTDIR is empty because the registry key is missing? "$INSTDIR" will become "" which means that everything from the system root could be deleted INCLUDING THE OPERATING SYSTEM." – someonr Mar 24 '22 at 16:32
1

RMDir on a directory without /r (recursive) flag will remove the directory if it is empty. At some point they have added this feature, not sure when.

NSIS Scripting Reference - RMDir

mcNux
  • 1,472
  • 1
  • 15
  • 13