Removing objects from collection with script works but gives an error
I have enhanced and modified your code and, it works perfectly :

bl_info = {
    &quot;name&quot; : &quot;Clear Collection&quot;,
    &quot;author&quot; : &quot;CédricF&quot;,
    &quot;description&quot; : &quot;&quot;,
    &quot;blender&quot; : (2, 80, 0),
    &quot;version&quot; : (0, 0, 1),
    &quot;location&quot; : &quot;Sidebar(N)&gt;Clean Up&quot;,
    &quot;category&quot; : &quot;Clean Up&quot;
}

import bpy

from bpy.props import PointerProperty

#----------------------------------------
#MAIN PANEL
#----------------------------------------

class CLEARCOLLECTION_Property(bpy.types.PropertyGroup):

    ClrByNameEnd : bpy.props.StringProperty(name= &quot;All Objects Ending By&quot;, default=&quot;&quot;)
    ClrByCOLNameEnd : bpy.props.StringProperty(name= &quot;Collection&quot;, default=&quot;&quot;)

class CLEARCOLLECTION_PT_MAINPANEL(bpy.types.Panel):
    bl_idname = &quot;CLEARCOLLECTION_PT_CLEARBYNAMEEND&quot;
    bl_label = &quot;Clear By Name End&quot;
    bl_space_type = &quot;VIEW_3D&quot;
    bl_region_type = &quot;UI&quot;
    bl_category = &quot;Clean Up&quot;

    def draw(self, context):
        layout = self.layout

        scene = context.scene

        CLRCTool = scene.CLRTool

        row = layout.row()
        row.prop(CLRCTool, &quot;ClrByNameEnd&quot;, icon= &quot;TRASH&quot;)
        
        row = layout.row()
        row.prop(CLRCTool, &quot;ClrByCOLNameEnd&quot;, icon= &quot;TRASH&quot;)

        layout.operator(&quot;clearcollection.clearbyend&quot;, icon= &quot;TRASH&quot;)
        layout.operator(&quot;clearcollection.slecbyend&quot;, icon= &quot;RESTRICT_SELECT_OFF&quot;)
        
#----------------------------------------
#CLEAR OBJECTS
#----------------------------------------

class CLEARCOLLECTION_OT_CLEAR(bpy.types.Operator):
    bl_idname = &quot;clearcollection.clearbyend&quot;
    bl_label = &quot;Clear Objects&quot;
    bl_description = &quot;Clears objects which end with the given input&quot;
    bl_options = {&quot;REGISTER&quot;}

    def execute(self, context):
    
        scene = context.scene

        CLRCTool = scene.CLRTool

        for obj in bpy.context.scene.objects: 
            if CLRCTool.ClrByNameEnd != &quot;&quot;:
                if obj.name.endswith(CLRCTool.ClrByNameEnd): 
                    bpy.data.objects.remove(obj)


        for obj in bpy.context.scene.objects:
            if CLRCTool.ClrByCOLNameEnd != &quot;&quot;:
                for objc in bpy.data.collections[CLRCTool.ClrByCOLNameEnd].all_objects:
                    if CLRCTool.ClrByNameEnd != &quot;&quot;:
                        if obj.name.endswith(CLRCTool.ClrByNameEnd): 
                            bpy.data.objects.remove(obj)
        
        return {&quot;FINISHED&quot;}

#----------------------------------------
#SELECT OBJECTS
#----------------------------------------

class CLEARCOLLECTION_OT_SELEC(bpy.types.Operator):
    bl_idname = &quot;clearcollection.slecbyend&quot;
    bl_label = &quot;Select&quot;
    bl_description = &quot;Selects objects which end with the given input&quot;
    bl_options = {&quot;REGISTER&quot;}

    def execute(self, context):
    
        scene = context.scene

        CLRCTool = scene.CLRTool

        for obj in bpy.context.scene.objects: 
            if CLRCTool.ClrByNameEnd != &quot;&quot;:
                if obj.name.endswith(CLRCTool.ClrByNameEnd): 
                    obj.select_set(True)


        for obj in bpy.context.scene.objects:
            if CLRCTool.ClrByCOLNameEnd != &quot;&quot;:
                for objc in bpy.data.collections[CLRCTool.ClrByCOLNameEnd].all_objects:
                    if CLRCTool.ClrByNameEnd != &quot;&quot;:
                        if obj.name.endswith(CLRCTool.ClrByNameEnd): 
                            obj.select_set(True)
        
        return {&quot;FINISHED&quot;}

classes = [CLEARCOLLECTION_Property, CLEARCOLLECTION_OT_CLEAR, CLEARCOLLECTION_OT_SELEC, CLEARCOLLECTION_PT_MAINPANEL]

def register():
    
    for cls in classes :
        bpy.utils.register_class(cls)
        bpy.types.Scene.CLRTool = PointerProperty(type= CLEARCOLLECTION_Property)


def unregister():
    
    for cls in classes :
        bpy.utils.unregister_class(cls)
        #del bpy.types.Scene.CLRTool


if __name__ == &quot;__main__&quot;:
    register()