It really depends on the type of worm on best way to get rid of them. Some you can kill with "meds" (betal nut extract), some aren't harmed by it. Some fish will eat, some fish ignore. Could remove all inhabitants and heat tank up above 100° F for a few hours or take the tank apart and dry everything out.
I picked up a second hand tank with substrate and about 12 newborn bloody mary shrimp. Tank was infested with detritus worms - the type that live in substrate and when they swim, it's like a snake - head steady, rest of body wriggles. I threw in some kuhli loaches and a pygmy cory and the worms disappeared. Endlers and guppies might go after the type on the walls. (not necessarily shrimp safe)