Permanently delete material

I try delete material on document:

            var textures = doc.Materials;
            foreach (var item in textures)
            { 
                doc.Materials.Delete(item); 
            }

this does not permanently erase, when the .3dm file is opened again on another pc, all textures will be created from the embedded folder again.

I need delete permanently from document.

Thanks

You need to remove also all materials from RenderMaterials table:

import scriptcontext as sc

mats = [mat for mat in sc.doc.Materials]
for mat in mats:
    sc.doc.Materials.Remove(mat)

rms = [rm for rm in sc.doc.RenderMaterials]
for rm in rms:
    sc.doc.RenderMaterials.Remove(rm)

Thank you @nathanletwory

Another question @nathanletwory

how can I ckeck if the material is being used?

I’d go over objects in document, get Id from RenderMaterial assigned to each object. Create also set with Id's of all RenderMaterials.

Here a sample script that shows one way of using sets to figure out what materials are used, what not.

import scriptcontext as sc

# sets to play with
all_mats = set([rm.Id for rm in sc.doc.RenderMaterials])
used_mats = set([o.RenderMaterial.Id for o in sc.doc.Objects if o.RenderMaterial])
unused_mats = all_mats - used_mats

print(len(all_mats), all_mats)
print(len(used_mats), used_mats)
print(len(unused_mats), unused_mats)

# for comparison now remove all material assignments and rerun used mats and
# unused mats sets
for o in sc.doc.Objects:
    if o.RenderMaterial:
        o.Attributes.MaterialIndex = -1
        o.CommitChanges()

sc.doc.Views.Redraw()

used_mats = set([o.RenderMaterial.Id for o in sc.doc.Objects if o.RenderMaterial])
unused_mats = all_mats - used_mats

print(len(all_mats), all_mats)
print(len(used_mats), used_mats)
print(len(unused_mats), unused_mats)

thank you @nathanletwory