A murder charge against a Farmingdale man was dismissed midtrial Tuesday because of prosecutorial misconduct so serious that Suffolk District Attorney Thomas Spota demanded — and received — the trial prosecutor’s resignation.

Prosecutors pulled the plug on the trial of Messiah Booker, 32, in the midst of a hearing in which defense attorney Brendan Ahern of Hauppauge was methodically detailing dozens of instances of evidence withheld by Assistant District Attorney Glenn Kurtzrock.

Kurtzrock’s supervisor, Homicide Bureau Chief Janet Albertson, replaced him in the courtroom to end the case. From the audience, Kurtzrock watched the end of the case and his career with the district attorney’s office — his face in his hands.

Booker instead pleaded guilty to second-degree attempted burglary and will get a sentence of 5 years in prison. He had faced a maximum of 25 years to life if convicted of second-degree murder in the Jan. 27, 2013, death of Demitri Hampton, 21, in Flanders.

State Supreme Court Justice John Collins said the plea deal was appropriate.

“The likely resulting sanctions [because of the misconduct] would dictate that the likelihood of [a murder] conviction be severely diminished,” Collins said.

The judge noted that what Kurtzrock did hurt Hampton’s family more than anyone. “They are the people who have been most severely wronged here,” Collins said.

Sign up for The Classroom newsletter. Education on Long Island is changing. Find out how. By clicking Sign up, you agree to our privacy policy.

The district attorney’s office agreed.

“The conduct of the assigned prosecutor in this case was inexcusable, resulting in further pain to the family of Demitri Hampton, and our prosecutors work tirelessly to make sure that does not happen,” spokesman Robert Clifford said. “Unfortunately in this case, we failed with a devastating result.”

Police and prosecutors had said Booker was part of a group that orchestrated a home invasion burglary looking for drug dealing money. Hampton, who was staying at the house, was shot and killed.

Ahern said he first noticed something was wrong last Wednesday, just before the last witness in the trial, Det. Brendan O’Hara, was about to take the witness stand. The detective’s notes were turned over as required before the trial, but Ahern said he noticed a two-year gap in the notes.

When he demanded and then received the rest of the notes, he said he realized Kurtzrock had been cherry-picking O’Hara’s notes, withholding evidence that at least two other men might have been responsible for the killing.

For more than two days, Kurtzrock and several supervisors in the district attorney’s office delayed Ahern from making that public during a hearing by asking for conference in the judge’s chambers. But the hearing finally started Tuesday morning.

“I take no satisfaction in this moment,” Ahern said. “There was a surgical extraction of exculpatory evidence. It was systematic and deliberate. ...The fact that multiple other people confessed [to taking part] should have been disclosed at arraignment.”

Prosecutors, Ahern said, had only themselves to blame, adding they “alone created this situation and must live with the consequences.”

Then Ahern began detailing, page by page, what evidence had been withheld. In addition to confessions, it included witness statements identifying other possible suspects. He was about two hours into an expected five-hour description when Albertson ended it.

Lawyers for Booker’s co-defendants said the misconduct affected them, too. Corry Wallace, 41, pleaded guilty to second-degree murder last month without knowing of this evidence, said his attorney Richard Stafford.

“None of this information was turned over to us,” he said, adding that his client may withdraw his guilty plea.

Ira Weissman, attorney for codefendant Danielle Hall, said he’s never seen misconduct so bad. “When something like this happens, it’s really hard to live with,” he said.

“This trial ultimately was not fair,” said Ahern, a former Nassau prosecutor who was trying his first case as a defense lawyer. “This family [the Hamptons] has been abused by the system. I doubt anything that came out of this trial can be described as fair and just.”