Run code when addon is disabled through preferences but NOT when blender closes I noticed that when it comes to addons, unregister() gets called both when disabling the addon AND when exiting blender.
I have an operation I only want to perform if the addon is disabled from the user Preferences, but NOT every single time blender closes.
Does anyone know of a way to differentiate between the two situations?
Some context:
my addon needs to copy custom matcap files to blender's user path (datafiles/studiolights). The problem is deleting them only when the addon gets unchecked in the preferences, but not every time blender exits. Copying those files into that user path seems to be the only way to install a matcap.
Not sure this is the best answer, but you can check if the current space is the Preferences windows in unregister
def unregister():
    if isinstance(bpy.context.space_data, bpy.types.SpacePreferences):
        # your code here

This should run the code when the user disables the addon from the prefs windows and it should not run the code when Blender is closed. In other cases it may or may not run, so this only works if &quot;best effort disable-detection&quot; is okay for you (it sounds like it is).