How to remove user installed add-ons in bulk

I admit that this is probably not a problem many people have but as an add-on developer I find myself every now an then in the situation that I want remove a whole bunch of user installed add-ons in one go.

What I often do when I develop a collection of add-ons is define a common category for all of them that is not one of the predefined categories. That way I can at least easily find them and see them grouped together in the user preferences.

Removing a single add-on is simple but to remove a bunch of user installed add-ons is less so because you have to locate the Blender user config directory (which is different on various operating systems) and you'll have to open each add-on file (or __init__.py file in a subdirectory if it's a multi-file add-on) to see if it defines the relevant category.

Tedious, but fortunately Blender can help. The code below shows you how. It is not an add-on itself, it is meant to be run from the command line inside Blender or from Blender's text editor (clicking Run Script). Removing stuff always carries the risk of accidental deletion so be careful (and use this snippet at your own risk. And keep back-ups, but careful people always do that, right? ). And yes, this code removes add-ons, not just disables them!


import bpy
from bpy.utils import script_path_user
from addon_utils import modules, module_bl_info

import os.path

userdir = script_path_user()

def remove_user_installed_addons(cat='Experimental development', dry_run=True):
    for mod in modules():
        if module_bl_info(mod)['category'] == cat:
            if os.path.dirname(mod.__file__).startswith(userdir):
                print("removing " + mod.__name__)
                if not dry_run:
                    bpy.ops.wm.addon_remove(module=mod.__name__)

remove_user_installed_addons(cat='Experimental development', dry_run=False)

As you can see, Blender provides us with an addon_utils module that has both a function modules() to produce a list of all add-ons (both enabled and not-enabled)  and a function module_bl_info() that returns the bl_info block of an add-on as a dictionary.
So all we have to do is loop over all installed modules, check if the module is part of the specified category and if so, use the script_path_user() function to determine if the directory that the add-on sits in, is in the user path (so we don't accidentally remove bundled Blender add-ons).
If it checks out, we user the addon_remove() operator to do the actual removal.

1 comment:

  1. Hi,

    what could be really cool is a script which a concept like this:

    Blender User Preferences -> Add-ons -> right click on 'Enabled' (or 'All' maybe?)it'll appear a menu with 8 options ('reset to default value';'unset';'copy to..' and so on).
    A script for a ninth option like "export a readme file containing a list of the enabled addons", inside the readme a pre-fix to separate add-ons already included in blender from third-parties add-ons. Maybe including something else like "this how you see XYZ addon named from the add-ons tab, that is how XYZ is named from the .py file".

    That way you make a list of names for your add-ons without using printscreen or traditional pen and paper. (a backup of the add-ons folder is an option but that will not tell you what addon was enabled or not)

    ReplyDelete