There are 20,000 species and only a few are harmful. most of them eat detritus. But its hard to identify the bad ones so we pretty much have to eliminate flatworms in our reef tanks.
There is a product called 'Flatworm Exit" but its recommended that you remove as many as you can before treating the tank. If you treat first, then you have a ton of flatworms dying and hurting water conditions. You can suck them out with a regular air hose.
A sixline wrasse would gobble them up too.
For the most part they are harmful, but not pretty once they grow in numbers