Thousands of drug cases tied to a state-run Amherst drug lab will be dismissed, and the state attorney general's office will bear the costs, the Massachusetts Supreme Judicial Court ruled Thursday. The decision is the result of the court's finding that prosecutors withheld evidence about a state chemist's wrongdoing.

Already, 7,500 cases linked to the same lab have been ordered dismissed by the SJC since April, after former state drug lab chemist Sonja Farak admitted to stealing and using drugs while working inside the lab.

But the latest case before the SJC went further. It sought to dismiss many more of the drug cases that came before the Amherst lab — not just the ones tested by Farak — because two former assistant attorneys general did not disclose evidence about the extent of misconduct by Farak — exculpatory evidence that could have been helpful to defendants.

"We conclude that Farak's widespread evidence tampering has compromised the integrity of thousands of drug convictions apart from those that the Commonwealth has agreed should be vacated and dismissed," Justice Frank Gaziano wrote in the decision. "Her misconduct, compounded by prosecutorial misconduct, requires that this court exercise its superintendence authority and vacate and dismiss all criminal convictions tainted by governmental wrongdoing."

It's unclear how many cases will be affected. The American Civil Liberties Union of Massachusetts estimated it could affect at least 12,000 cases — possibly more.

Rebecca Jacobstein, with the Committee for Public Counsel Services — the state public defender — said the decision goes beyond the important step of securing justice for thousands of people.

“It also sets a precedent," she said. "It says, this behavior is unacceptable. We will dismiss cases when prosecutors don’t play by the rules. And we will hold them accountable.”

The decision says the new "Farak defendants" will include: those convicted of methamphetamine offenses while Farak worked at the Amherst lab, and any defendants who had drugs in their cases tested between January 2009 and January 2013, when the lab closed.

The attorney general's office will have to pay for the costs related to the dismissal, including providing notice to thousands of people whose cases will be dismissed. Gaziano wrote that the office is the one responsible for the prosecutorial misconduct — and the additional dismissed cases.

In a statement Thursday, a spokeswoman for Attorney General Maura Healey said the office is "grateful to the court for its thoughtful opinion and its commitment to justice for the Farak defendants."

"We share that commitment and welcome the role the court has given our office to help facilitate notice to impacted defendants and secure comprehensive and speedy relief," the statement said. "AG Healey and her administration are fully committed to ensuring that the mistakes of the past never happen again.”

The two former assistant attorneys general did not work under Healey, but rather former Attorney General Martha Coakley.

The court ruled new checklists should be created to clarify what might be considered exculpatory evidence. That checklist would emphasize that when a prosecutor is unsure if something should be turned over to defense attorneys, that the attorney will have to present the information to a judge to review in chambers.

In a separate Massachusetts drug lab scandal in 2012, more than 20,000 cases were dismissed after another chemist, Annie Dookhan, admitted to falsifying drug tests at the Hinton lab where she worked.

For Jacobstein and other defense attorneys, the work is just beginning on the newest Farak defendants. She said now, lawyers need to identify who is still behind bars, and who needs help cleaning up their criminal record and getting their life back in order.

"There’s a lot of work to be done," she said. "But we are incredibly pleased to have all this work to do, to get these people the justice they deserve and to be able to move on from this disaster.”