A man who stabbed his ex-wife’s attorney more than 30 times in her Fridley law office in 2010 was tackled to the floor Monday by sheriff deputies during an outburst at his plea hearing.

Sheikh Nyane, 36, became upset and started pounding on a table when asked to admit, as part of a plea agreement, that he attempted to kill family law attorney Terri Melcher, according to authorities and Melcher. Although his legs and hands were shackled, the deputies moved quickly to restrain Nyane, who is 6 feet 3 inches tall and weighed 240 pounds according to his most recent driver’s license.

Nyane later agreed to a plea that will carry a maximum prison time of nearly 17 years, about a year less than he could have received if he were convicted at trial. He was charged with felony first-degree attempted murder and will be sentenced Jan. 9.

“This was a giant step toward closure,” said Melcher. “I’m fine with this plea, and I don’t have to go to trial.”

Authorities said that Nyane attacked Melcher, 58, in June 2010 in a fit of rage over an unfavorable child custody ­ruling. She lost half her blood before surgery saved her life.

Psychologists deemed Nyane competent and incompetent to stand trial in four evaluations. He spent more than a year at the Minnesota Security Hospital in St. Peter.

During Monday’s hearing, he became agitated a second time, but deputies were standing nearby. After a lunch break, he apologized for his outbursts and entered his guilty plea on the record, said Anoka County Attorney Tony Palumbo.

Judges usually abide by plea agreements because the prosecution and defense have settled on a sentence, he said. Nyane’s crime was heinous and Melcher was close to death, Palumbo said.

“Such outbursts in court, especially at a plea hearing, are very unusual,” he said.

Melcher, who had attended every pretrial hearing in her case, was sitting two rows behind Nyane in the courtroom gallery, she said. He started pounding on the table, the defense attorneys scrambled and the deputies jumped in, she said.