If you want to do it inusing Blender's Python API you can executeuse the following. Just make sure you unlink the unwanted materials before doing this:
toRemove = [block for block in bpy.data.materials if block.users == 0] for block in toRemove: bpy.data.materials.remove(block)