Toilet flange repair isn’t easy. There are a couple of possibilities, and everything depends on the size and type of pipe. If the pipe has a 4-inch inside diameter, it's an easier task. You simply buy an inside closet flange. This flange is designed to glue on the inside of the pipe or expand inside the pipe. You could then leave the old flange right in place and glue on the new one at the right elevation for the floor, if it's plastic. The flange should be sitting with the bottom edge flush with the top of the floor. If it isn't, then you will have to cut away the old flange.
If the plastic pipe has a 3-inch inside diameter, you’ll have to remove the old flange from the outside of the pipe. This is a difficult task because you have to make the opening around the flange large enough to peal the old flange off the pipe. In addition, there is no guarantee of success with this type of toilet repair; it takes a plumber many years to master the art of splitting the glue joint without damaging the pipe.
If a closet flange is cast iron, then you would have to use a hammer and chisel to break the flange and remove it. If it is a 4-inch pipe, then you can use an insert closet flange that has a rubber gasket that expands on the inside to seal against the inside pipe surface. You will have to remove all the old build-up in order for it to seal. This can be done by scraping or sanding with a wire brush on a drill. Use a little silicone on the gasket when you begin tightening it up. If it is a 3-inch cast iron pipe, you will have to break off the old flange from the outside and install another one on the outside. This can be done with a hammer and chisel. If the flange is cast iron with a rubber gasket, you should be able to remove the cast iron with a hammer and chisel, remove the rubber gasket with a sharp knife, then replace the gasket and the flange.
If the closet flange is lead and installed on the outside of the pipe, you will have to remove the old flange and re-caulk it, and then pour a new lead joint. You can also install an expanding one on the outside, but that would require breaking up the floor to get the flange down into the floor.
After setting every flange, make sure you screw it to the floor so it will not rise up when you tighten down the toilet. Bolt the flange down through the holes supplied in the flange with concrete or wood anchors that are galvanized or brass so they won't rust and break. And when you tighten it, never use more than a 6-inch crescent and two fingers on the handle to get the nuts tight. In addition, if you use a plastic flange, make sure you use one with a stainless steel outer ring, so it won't rust and is stronger than the plastic itself. Again, no toilet should be tightened so tight it breaks the flange. A toilet should always be set level and the edge that touches the floor should be sealed with calk to prevent it from smelling.