Prosecutors in eight counties around Boston announced Tuesday that they would dismiss more than 19,000 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 first Massachusetts 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’d done. Dookhan pleaded guilty to multiple counts of tampering with evidence and obstruction of justice and served three years in prison.

Prosecutors said Dookhan admitted testing only a fraction of a batch of samples, then listing them all as positive for illegal drugs. Her motive, they said, was to boost her productivity and burnish her reputation.

The state public defender service and the American Civil Liberties Union said that individually relitigating 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 Massachusetts 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 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 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 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.

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.