How to delete isolated shader nodes from script?
Remove unlinked nodes.
Re &quot;totally isolated&quot; nodes ie no links in no links out
Go thru all links and add the to and from nodes to the linked node set. The unlinked nodes is the difference between all nodes and linked nodes.
import bpy

context = bpy.context
ob = context.object
mat = ob.active_material
#mat = bpy.data.materials.get(&quot;Foo&quot;) # material Foo
assert (mat is not None and mat.use_nodes), &quot;No material or not node based&quot;
linked_nodes = set()

for link in mat.node_tree.links:
    linked_nodes.add(link.from_node)
    linked_nodes.add(link.to_node)
        
unlinked_nodes = set(mat.node_tree.nodes) - linked_nodes
while unlinked_nodes:
    mat.node_tree.nodes.remove(unlinked_nodes.pop())