| projects | articles | photos | about

How to run a clean-up script when your extension is uninstalled

Right now there is no support in the Firefox Extension Manager for running a script when the extension is uninstalled. Such script might be useful if your extension needs to perform some clean up, like removing some temporary files or registry entries.

I’ve managed to find a work around for this:

  1. Install an unload event handler in your overlay Javascript:

    addEventListener("unload", overlayUnloaded, false);

    The overlayUnloaded function will be called each time the user closes the browser window.
  2. In the overlayUnloaded function, ask the extension manager (nsIExtensionManager) whether your extension is in toBeUninstalled state.
  3. If so, the user has uninstalled your extension – go ahead and run your clean-up code.

A related technique is determining that your extension has been updated, and something went wrong.

I can provide the complete source code (in Javascript) for this technique on request.

8 Responses to “How to run a clean-up script when your extension is uninstalled”

  1. Jed Says:

    Awesome, I woul definitally like this for my QuickNOte/BBCode and WebMailCompose extensions, as the firefox uninstaller doesn’t get rid of prefs that I want removed.
    Thanks once again alex.

  2. mark Says:

    Thanks too. I was looking for a post uninstall extension script or event but soon realized it didn’t exist yet. I would appreciate seeing code on using nsIExtensionManager for this purpose. Let’s hope an onuninstall event is added to make handling easier.

  3. Duane Fields Says:

    Could you send me your example “uninstall” extension hook code?

  4. Iosart Says:

    The code for this technique can be found in my FirefoxView extension.

  5. Firefox Fanatic Says:

    I’m not any big programmer or anything but sometimes when i uninstall an extension from firefox and i check my profile folder i still see folders containing scripts n other stuff from the uninstalled extensions.. I’m not sure if this is supposed to be like tht, but if the solution u stated above can clean up those unnecesary files then yes it is a good idea that should be followed up

  6. Gergo Says:

    Great idea,
    I was looking for this functionality and found ‘Adding entries to the uninstall log’ on MozillaZine. Unfortunately that method needs to add each file separately to the Uninstall file in the extension directory. In my case I save a lot of web pages (implementing my own cache) and it would be too much files to add to the list. It seems to be much easier to delete my cache’s root directory recursively from a script on uninstall.
    Thanks a lot.

  7. Hurry Says:

    Nice concept, but I am a little dumb here. I am a newbie, could you give me complete step by step to do this clean up with the code? I really appreciate your help.

  8. ayembee Says:

    nice… at the moment i have a strong interest in something that works from the other direction — is it possible to run code on installation that can clean out an old version of your extension? (if a change of GUIDs becomes necessary, for instance)