If you want to do it using Blender's Python API you can use 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)