SAN FRANCISCO – A federal judge approved the largest auto-scandal settlement in U.S. history Tuesday, giving nearly a half-million Volkswagen owners and leaseholders the choice between selling their cars back or having them repaired so they don’t cheat on emissions tests and spew excess pollution.

The German automaker acknowledged last year that about 475,000 Volkswagens and Audis with 2-liter, four-cylinder diesel engines were programmed to cheat on emissions tests.

Under the agreement, owners can choose to have Volkswagen buy back the vehicle regardless of its condition for the full trade-in price on Sept. 18, 2015, when the scandal broke, or pay for repairs. In either case, Volkswagen also will pay owners $5,100 to $10,000, depending on the age of the car and whether the owner owned it prior to Sept. 18 of last year.

Volkswagen has agreed to spend up to $10 billion compensating consumers and could start buying back the cars as early as next month. Regulators have not approved any fixes. The settlement also includes $2.7 billion for unspecified environmental mitigation and $2 billion to promote zero-emissions vehicles.

In this Sept. 22, 2015, file photo, the amount of carbon dioxide emissions is written on a Volkswagen Passat Diesel at the Frankfurt Car Show in Frankfurt, Germany. Photo: AP/Michael Probst

U.S. District Judge Charles concluded that affected car owners were not entitled to a full refund because many had “received a great deal of use out of their vehicles.” He also raised the specter of bankruptcy for Volkswagen if it had to pay the full purchase price.

The scandal has damaged Volkswagen’s reputation and hurt its sales. The company is still facing potentially billions more in fines and penalties and possible criminal charges. It also will pay up to $324 million in attorney fees and $8.5 million in out-of-pocket costs, and has agreed to pay its U.S. dealers up to $1.2 billion.

The lead attorney for car owners, Elizabeth Cabraser, said in a statement that the deal “holds Volkswagen accountable for its illegal behavior and breach of consumer trust.” More than 330,000 people have signed up for settlement benefits, with about 3,200 opting out, she told the judge last week.

The company said in April that it has set aside $18.2 billion to cover the cost of the global scandal, which erupted in September 2015 when the U.S. Environmental Protection Agency said Volkswagen had fitted many of its cars with software to fool emissions tests. Car owners and the U.S. Department of Justice sued.

The software recognized when the cars were being tested on a treadmill and turned on pollution controls. The controls were turned off when the cars returned to the road. The EPA alleged the scheme let the cars spew more than 40 times the allowable limit of nitrogen oxide, which can cause respiratory problems in humans.

The settlement releases legal claims from most of the 2-liter engine owners, but it doesn’t affect larger 3-liter, six-cylinder diesels, which also cheated on tests. The settlement also doesn’t end any claims against parts supplier Robert Bosch, which drew up the cheating software.