Get icon_id of image already loaded
i'm not sure without seeing your code ( a github link would be great ). I can make a guess about how you might be able to avoid the KeyError

# ImagePreviewCollection(dict) is a subclass of Python's built-in dict type
main_collection = icon_collection["main"]
icon = main_collection.get(some_key)
if not icon:
    main_collection.load(icon_name, icon_path, 'IMAGE')


I use custom icons in mesh_tiny_cad

If i'm reading bpy/utils/previews.py correctly it isn't possible to overwrite or clear a key/value pair.



If you are keeping track of the IDs somehow then..

if icon_id in [v.icon_id for k, v in pcoll.items()]:
    ...