Former Massachusetts drug lab chemist Annie Dookhan, in Suffolk Superior Court in Boston, before a hearing where she entered a guilty plea on charges of obstruction of justice, perjury and tampering with evidence. (2013 pool photo by David L Ryan/Boston Globe via Associated Press)

Prosecutors in eight counties around Boston revealed Tuesday that they would dismiss 21,587 drug cases tainted by the misconduct of Massachusetts state drug lab chemist Annie Dookhan over a period of eight years, although nearly all of those convicted in her cases have already served their sentences.

The beginning of the end of the state’s first drug lab scandal — another chemist in western Massachusetts may be responsible for just as many flawed cases — came five years after Dookhan’s actions were discovered, and over the strenuous objections of state prosecutors, who pushed for individual reconsideration of Dookhan’s cases rather than mass resolution. Dookhan admitted falsifying reports, contaminating samples intentionally, grouping multiple samples together or simply not doing drug testing she certified she had done. Dookhan pleaded guilty to multiple counts of tampering with evidence and obstruction of justice and served three years in prison.

The state public defender service and the American Civil Liberties Union said that individually re-litigating 24,000 cases, with 20,000 defendants, would be both overwhelming to the state’s defense bar and pointless in light of what they called an unfair and ineffective war on drugs. So the Supreme Judicial Court, the state’s highest court, came up with a solution, after declaring Dookhan’s deeds “government misconduct that has cast a shadow over the entire criminal justice system.” The court, in an opinion written by Chief Justice Ralph D. Gants, also acknowledged the ongoing impact of drug convictions on defendants’ abilities to find housing, jobs or financial aid.

The court in January ordered the district attorneys in all eight counties which received test reports from Dookhan to review her cases, determine which ones could or should not be retried, and present a list of all cases to be dismissed or preserved in 90 days. The court urged prosecutors to consider “whether a conviction warrants burdening the court system” with retrials, and more payments to defense attorneys. The prosecutors released that list Tuesday.

“Today is a major victory for justice and fairness, and for thousands of people in the Commonwealth who were unfairly convicted of drug offenses,” said Matthew Segal, who helped lead the litigation for the ACLU of Massachusetts. “Unfortunately, the victims of this crisis waited far too long for justice. It shouldn’t have taken years of litigation by the ACLU, public defenders, and pro bono lawyers to address this stain on the Commonwealth’s justice system.”

The most cases were in Suffolk County, where Boston is located. Suffolk District Attorney Daniel F. Conley said in a news release Tuesday afternoon that his office would dismiss “viable drug convictions” against 7,886 defendants involving more than 15,500 charges, each of which he said had corroborating evidence beyond Dookhan’s drug testing. But he said he was making a good-faith effort to abide by the supreme court’s guidance to winnow down the number of remaining Dookhan-related cases, and that even if a case were reopened probation or drug treatment could not be reimposed.

Conley said he would fight to preserve 117 of the most serious cases, or about 1.5 percent of Suffolk’s total number of Dookhan-related cases. He said those defendants had sustained more than 1,700 convictions for violent or weapons crimes, and that his office still had sufficient evidence to retry the cases if needed.

About 600 cases in Suffolk had already been resolved on motions by lawyers in the years since Dookhan’s actions were discovered in 2012. In addition, prosecutors found 18 cases where Dookhan certified a substance as illicit when it was not. Jake Wark, a spokesman for the Suffolk district attorney, said, “In every one of those cases we moved immediately to free the defendant if he was held and terminate any prosecution, and none of them involved a person held on bail or serving a sentence solely for a Dookhan drug case.”

Some “‘Dookhan defendants” served several years in prison, and though they are now freed, “they continued to suffer the harsh collateral consequences of their tainted convictions,” including immigration status for some, said lawyer Daniel Marx, whose firm also represented the Dookhan defendants. “Now, a majority of these wrongfully convicted individuals will have the opportunity to clear their records and move on with their lives.”

The list of dismissed cases was only the first step in redressing Dookhan cases. Next, the prosecutors must send a court-approved letter to those defendants whose cases are not dismissed, notifying them of the opportunity to reopen their cases and also providing a “hotline” phone number to public defenders for guidance. The Committee for Public Counsel Services, which appoints defense lawyers to the indigent, will be allowed to insert a separate letter providing further legal advice. A form enabling indigent defendants to apply for an appointed lawyer will also be included, and all of it must be sent with a self-addressed stamped envelope — all at the government’s expense.

“We recognize that this cost could be considerable,” Gants wrote, “but that is a consequence of egregious government misconduct that affected more than 20,000 defendants.”

And if the number of defendants who respond to the letters is overwhelming, and the CPCS cannot find enough attorneys to handle the cases, the supreme court will consider simply dismissing their cases.

And none of this addresses the case of Sonja Farak, who has admitted actually using large amounts of the drugs that were submitted to her lab in western Massachusetts. She was convicted of tampering with cocaine evidence, that she was supposed to be testing, and sentenced to 18 months in prison. Some defense attorneys maintain that prosecutorial misconduct kept the details of Farak’s drug-induced work secret for almost two years after her problems were discovered. The process of unraveling how many cases were tainted by Farak’s misconduct is continuing.