How to cleanup material table using IO File3dm class

Not at all, im trying to export blocks from file to independent files - i tried to write 3dm on my own using File3dm - not possible as blocks are losing its contents so ok I found a workaround to export those intact using RhinoDoc.Write with WriteSelectedOnly option but this leads to a huge mess in files so I wanted to handle cleanup on my own right after model file saving but ended up with file3dm empty material table and unnecessary materials in it - those are exactly the same as in the original file not less not more exact same count in material tab and those remains unchanged.

I thought about it but one of those mats is normal rhino custom mat but with texture instead of pure color.