SAN JOSE –Three million dollars in legal fees, two-plus years and a slew of nasty tabloid articles later, multimillionaire Clyde Berg on Friday was declared factually innocent of a horrific sexual crime that a judge called an “elaborate fraud” concocted by his wife.

“I apologize on behalf of the entire system, which has victimized you in this case,” Judge Ron M. Del Pozzo told Berg, 75. “I can’t imagine what you have gone through.”

“Thank you, your honor,” said Berg, standing up in court in a black polo shirt and khakis to acknowledge his gratitude.

Friday’s rare finding of factual innocence means the incident will be wiped off the real estate mogul’s record, as if it never existed. He faced charges that could have put him in prison for life after his 37-year-old wife, Ellena, claimed in 2012 that he outfitted her while she was nine months pregnant with a spiked dog collar and leash, chained her at gunpoint to a bed for two days and raped her repeatedly with his golf putter.

Prosecutors dropped the charges about a year later after a dramatic five-day preliminary hearing that ended with Del Pozzo dubbing the tall, thin Swedish woman “not worthy of belief,” and concluding that Berg was the victim of a greedy scam aimed at getting around a restrictive prenuptial agreement.

But Berg’s travails are not over. The state Attorney General’s Office is still investigating Ellena Berg’s complaint that District Attorney Jeff Rosen abused his discretion by dropping the charges. And Berg is still fighting a 43-page civil lawsuit she filed shortly after his arrest, claiming he physically and sexually abused her for a decade and seeking punitive damages from his $200-million-plus fortune.

Clyde Berg is the brother and business partner of billionaire Carl Berg, one of the richest people in the United States. The brothers developed campuses for many of the valley’s signature companies and recently sold their firm for $1.3 billion.

In a phone interview after Friday’s hearing, Clyde Berg said he was “very happy” with Del Pozzo’s decision but weary of the whole mess.

“The bottom line,” he said, “is that none of this should have happened.”

Clyde Berg spent about eight days in jail, posted $3 million in bail and wore a GPS ankle bracelet while he fought the charges. He also was locked out of his house for six months and lost custody of his dog, Rudy, after his wife claimed he abused the animal.

Even after the charges were dropped, the British tabloid press continued to publish photos of Ellena Berg lying in her hospital bed after the ersatz incident, her face red with what appears to be blood or lipstick.

In other repercussions, the Attorney General’s Office has yet to announce whether Ellena Berg’s complaint is justified. Legal observers expect Rosen to be vindicated — perhaps by the middle of next month. The Attorney General’s Office declined to comment.

Ellena Berg has lost her claim in divorce court this fall, meaning Clyde Berg no longer has to pay her monthly support, which cost him up to $25,000 a month for a total of $550,000. He also has had to pay millions to cover her legal fees as well as his own.

Her lawyers recently moved to add a new accusation to the civil case, claiming that Clyde Berg hired two men this fall to beat her up in Sweden, where she was visiting her family. He denies it. Ellena Berg could not be reached for comment.

The case also could have legal consequences for her. Sheriff’s deputies are investigating allegations of extortion, filing a false police report and other offenses.

Berg has criticized Rosen for charging him, saying he threw the book at him without thoroughly investigating the case in hopes he’d plead guilty to lesser charges.

But prosecutors have said that to assess Ellena Berg’s credibility, the office had to “flush it out in the most public of forums,” open court. Ellena Berg contends she is suffering from post-traumatic stress disorder, and sources familiar with the case said that Ellena Berg’s lawyers urged the office not to bow to pressure from a rich and powerful man.

Friday, Clyde Berg’s lawyer, Kurt Seibert, said his client’s quest to clear his name began the instant he was arrested in the middle of a September night more than two years ago. Waiving his right to a lawyer, Berg allowed deputies to interrogate him for five hours.

Prosecutor Catherine Pozos told the judge Friday she could find no justification for denying the petition.

“I found none,” she said. “I have nothing. I’m inviting the court to grant the petition.”

Del Pozzo on Friday branded Ellena Berg a “liar” and said the case reminded him of one decades earlier that resulted in the misdemeanor conviction of a woman for filing a false rape report against a Stanford student.

“Your wife perpetrated an elaborate fraud that has caused you great suffering,” the judge said. “I hope you will stay strong until all this is over and the nightmare finally ends.”

Contact Tracey Kaplan at 408-278-3482. Follow her at Twitter.com/tkaplanreport.