The uninstallation behavior of Windows Installer is actually quite efficient. At uninstall time, Windows Installer will only remove the items that have been installed by the package unless the component containing the resources has been marked as permanent.
At times, this can pose a problem for the setup developer. Applications will often create files as part of normal everyday usage. Since these resources did not belong to the original installation, they will remain during an uninstall. The same applies for registry keys as well.
The functionality is there to avoid such a situation. Windows Installer has a RemoveFile table which allows you to remove files and folders that were not part of the original installation. Some people believe that the RemoveRegistry table has the same function as the RemoveFile table. This is not the case. Yes, the RemoveRegistry table does remove registry keys, BUT only during the installation of the component that the RemoveRegistry resource belongs too. In order to remove registry keys that aren't part of the original installation at uninstall time, you will need to use the Registry table.
Populating these tables is quite simple. The Windows Installer SDK contains detailed information on how to add entries to these tables, but for example I will show you how.
The RemoveFile table has 5 columns, FileKey, Component_, FileName, DirProperty and InstallMode. Notice the Component column has an underscore after the name. This is to indicate that the entry in this column is a foreign key in to the Component table. Here is are some examples of RemoveFile table entries, following will be an explanation of each:
RemoveTempFiles001 | RemoveFile | * | FOO | 2
RemoveAppDir | RemoveDir | {NULL} | INSTALLDIR | 2
The first example shows how to remove files from directory Foo at uninstall time. The FileKey entry is arbitrary, you can call it anything you want, but it must be unique. The Component column must reference an already existing component in the installation. The FileName column can either be the localizable filename of the file you want to remove, a wildcard, or null. If it is null, the directory specified in the DirProperty column will be removed if it is empty. The DirProperty column specifies a directory from the Directory table that contains the file(s) to be removed or the directory to be removed. The InstallMode column is when the file(s) will be removed. 1 means during install, 2 is during uninstall, 3 means either case. The second example shows how to remove a directory during uninstall; again, this is assuming that the directory is empty.
I will speak more on removing orphaned registry keys in the next post.