If we stop breeding pigs and cows we won't have the huge amount we do now. The pigs and cows don't care if their species go extinct.
Letting them go into the wild would be cruel since they aren't prepared to exist out there, but letting them die in the wild vs die in a factory farm isn't much worse of a fate.
In reality, we should stop breeding them, send existing aninals to sanctuarys (which we should fund) and be done with it. Within 20 years, all of the existing farm animals will die out from natural causes.
The real moral question is: how do we support the farmers?
We should ensure that they get their wages replaced and get assistance in moving to a less cruel industry than animal farming. There are already groups that help with this, but they'd need funds to scale their work.