An Iowa man who was set to plead guilty in a sex abuse case was freed from jail Monday after a judge cited a drunken county attorney for failing to prosecute him in a timely manner.

The defendant had been charged with third-degree sexual abuse and disseminating obscene material to a minor by telephone before District Court Judge Martha Mertz ruled that the charges should be dropped, the Des Moines Register reported.

In her ruling, Mertz cited Clarke County Attorney Michelle Rivera, who was arrested Oct. 18 for being intoxicated inside an Osceola courtroom.

CLICK HERE TO GET THE FOX NEWS APP

Prosecutors missed the deadline the next day to provide the defendant with his right to a speedy trial or file for an extension.

"The county attorney’s unavailability at the last hearing was the finale following unexplained periods of inactivity and lack of responsiveness that prevented disposition of this case within one year," Mertz said, citing Rivera's unavailability at a recent hearing in the case.

The defendant was arrested in September 2017 on suspicion of sexually abusing a 13-year-old boy. He admitted to engaging in sexual activity with the teen, according to a criminal complaint.

He sought to have the case dismissed because prosecutors had not been tried within a year of his arraignment. He wound up spending 15 months in jail.

A plea deal was reached, but Rivera was arrested right before the October plea hearing.

A Clark County Sheriff’s deputy noticed Rivera "slurring her words and stumbling on her feet" in the courtroom. She was arrested after refusing to take a breathalyzer test.

She pleaded guilty to the courthouse intoxication and was fined $65.

DEMOCRATIC COUNTY ATTORNEY APOLOGIZES AFTER ALLEGEDLY BEING CAUGHT DRUNK IN COURTROOM

Defense attorney Marshall Orsini, told local media outlets that his client's release was long overdue.

"If the defendant is not put on trial (and) is not tried within a year, you have the potential to have that case dismissed, and that's what happened here," Orsini said.

Rivera, a Democrat, lost her re-election bid for county attorney in November.

She was also arrested again last week on suspicion of operating a vehicle while intoxicated and child endangerment. A deputy determined she was intoxicated while dropping off her daughter off at daycare.

She was expected to make a court appearance Thursday.

The Associated Press contributed to this report.