Removing all vertex color from all/one selected objects

What i need: i want to make a script which will remove all vertex color data in both cases:
several selected objects with several vertex color data
or only one selected object (which also can contain multiple Col.).

Im trying this script:

import bpy

for obj in bpy.context.selected_objects:
    for col_attr in obj.data.vertex_colors:
        obj.data.vertex_colors.remove(col_attr)

But it cases when some object have something like:
Col
Col.001

Script can throw an error in some cases:
Traceback (most recent call last):
File “\Text.001”, line 5, in
RuntimeError: Error: Vertex color ‘NGon Face-Vertex’ not found

You’re removing items from a dictionary while iterating over it. use vertex_colors.items() instead

1 Like

Can you show some examples with vertex_colors.items()? :face_with_monocle:

Okay i find the answer:

import bpy

for o in bpy.context.selected_objects:
if o.type != ‘MESH’:
continue
while(o.data.vertex_colors):
o.data.vertex_colors.remove(o.data.vertex_colors[0])

for k,v in obj.data.vertex_colors.items():
    obj.data.vertex_colors.remove(v)