A federal judge on Tuesday tossed a $79 million settlement between American Express and a group of retailers after it was discovered that two ethically challenged lawyers on the case shared confidential documents via e-mail.

The “improper and disappointing conduct” by the lawyers — one representing the stores and the other representing MasterCard — “fatally tainted” the negotiations leading to the settlement, Judge Nicholas Garaufis wrote in his 44-page decision.

The two bone-headed lawyers, Gary Friedman, counsel to the stores, and Keila Ravelo, who represented MasterCard, were old friends and were in “frequent” contact, the judge wrote.

The stores had inked a separate, $6 billion settlement with Visa and MasterCard that Ravelo helped craft.

The fate of that settlement is up in the air as lawyers for the stores are now trying to scuttle it — citing the Friedman-Ravelo e-mails and the possibility that the credit card company has unfair, confidential info.

Friedman apparently knew his contact with Ravelo put him waist-deep in an ethical swamp as he wrote in the e-mails to his pal — who worked for the powerful Willkie Farr & Gallagher firm — to “burn after reading.”

Judge Garaufis dismissed Friedman as the lead lawyer for AmEx. The retailers’ remaining lawyers have until Sept. 8 to file new papers arguing why they should be able to stay on the case.

“We are disappointed in the court’s decision to deny final approval of the settlement,” AmEx spokeswoman Marina Norville said in a statement. “We continue to believe the agreement was fair to merchants, and would provide them with additional flexibility while ensuring our Card Members are treated fairly at the point of sale. We believe we have strong defenses against the merchants’ claims, and will continue to fight our case in court.”

It’s not clear what Ravelo or Friedman stood to gain, but the two have been close friends since 1992, and “apparently seriously considered the purchase of a Gulfstream II jet and the operation of an air charter business,” Garaufis wrote in his order.

The explosive e-mails might have gone undiscovered if Ravelo hadn’t allegedly tried to scam Willkie Farr out of $5.75 million beginning in 2008.

Ravelo and her husband allegedly formed sham consulting companies that billed the law firm for work never performed, court papers charge.

Ravelo and her husband, Melvin Feliz, were arrested in December at their $2.3 million Englewood Cliffs, NJ, residence on wire fraud charges.

Willkie discovered the Friedman-Ravelo e-mails while investigating the scam — and then promptly handed them over to the court .

“We are not in a position to comment at this time,” Steven Sadow, a lawyer for Ravelo, told The Post.

Ravelo is out on bail.

Feliz was sentenced to 10 years in prison on separate drug-trafficking charges.