The Justice Department announced last week that it would not retry Senator Robert Menendez of New Jersey, eliminating for Democrats the embarrassing prospect of having a sitting senator running for re-election while on trial for corruption. Bryan Anselm for The New York Times

Five days after the Department of Justice announced that it would retry Senator Robert Menendez of New Jersey, the judge in the case all but ripped out its core. Prosecutors, he said in an 53-page opinion, had failed to prove that the senator, a Democrat, had accepted hundreds of thousands of dollars in campaign contributions in exchange for lobbying on behalf of his co-defendant.

The government’s allegations of campaign contributions for favors performed were “empty of relevant evidential fact,” Judge William Walls wrote. “There is no there there.”

The department was forced to conclude that the same was now true of its entire case. In a whiplash decision, it announced that it would not, in fact, retry Mr. Menendez, eliminating for Democrats the embarrassing prospect of having a sitting senator running for re-election while on trial for corruption.

The judge’s decision further exposed what the bribery case against Mr. Menendez and Dr. Salomon Melgen, a wealthy eye doctor from Florida and a longtime friend of the senator, lacked from the start: no key witness, no obvious quid pro quo. He left in place 11 of the 18 charges, including bribery based on trips on a private plane and other lavish gifts, as well as allegations that Mr. Menendez made false statements about those gifts — a charge even defense lawyers conceded was the hardest to beat.

But Judge Walls blew a hole in the government’s timeline and bounty of evidence, and made it hard to argue a motive for making false statements. The defense, meanwhile, still had its steady refrain: this was friendship, not corruption.

“The best the government could ever hope for was a hung jury,” said Lawrence S. Lustberg, a defense lawyer in Newark, where the first case was tried and, in fact, ended in a mistrial when jurors said they could not reach a unanimous verdict. “The summation is, ‘If exchanging gifts between friends is a crime, then we’re all in trouble.”

Justice Department officials in Washington had debated whether to even retry Mr. Menendez, but eventually reached a consensus. Attorney General Jeff Sessions, a Republican who served alongside Mr. Menendez in the Senate, signed off on a recommendation from the deputy attorney general and the criminal division to try again to win a conviction.

Some in the department had been uneasy with the case from the beginning. It started with an investigation into salacious rumors that Mr. Menendez and Dr. Melgen had visited underage prostitutes in the Dominican Republic. The charges had been peddled to news outlets for years by an anonymous informant who adopted his pseudonym from a former congressman from New Jersey, Pete Williams, who was convicted of bribery in the Abscam case of the 1970s.

Reporters and government investigators alike could never substantiate those rumors. The government shifted instead to more complex, less sexy matters involving Medicare billing and port security contracts.

The Justice Department’s Public Integrity Unit, which had suffered through an embarrassing string of losses and missteps involving prosecutions of high level politicians like former senators Ted Stevens of Alaska and John Edwards of North Carolina, was eager to burnish their once-lofty reputation.

The prospect of a corruption case against another sitting senator, especially for bribery, proved tantalizing.

“I think that there was a little bit of zealousness in trying to find the next big fish in order to set things right reputationally about the public integrity section,” said Brian Fallon, who worked as a spokesman for the Department of Justice before joining Hillary Clinton’s presidential campaign in 2015. He also briefly worked for Mr. Menendez. “I think that certain people were seeking some kind of big name that they could deliver a case all the way to a conviction.”

Justice Department officials declined to discuss any details of their initial decision to retry the case.

The indictment was handed down in April 2015. But less than a year later, before a trial date had even been set, the Department of Justice was dealt the first blow to their case: the Supreme Court overturned a corruption conviction against former Virginia governor Bob McDonnell, extremely narrowing the definition of official acts that would qualify for a bribery conviction.

Still, prosecutors were not overwhelmingly concerned about how the ruling affected the Menendez trial, thinking that their burden was more on proving that Mr. Menendez had accepted bribes than it was on proving that he had taken actions.

The decision further exposed what the bribery case against Mr. Menendez and Dr. Salomon Melgen, a wealthy eye doctor from Florida and a longtime friend of the senator, lacked from the start: no key witness, no obvious quid pro quo. Lannis Waters/Palm Beach Post, via Associated Press

Judge Walls, though, repeatedly questioned whether the McDonnell decision would allow the introduction of certain evidence in the first trial. And the question of whether the senator’s actions were official acts added another level of complexity for the jury.

Early in the trial, Judge Walls made it clear that he was not going to let any of the salacious rumors work their way into his courtroom, nor what he viewed as loaded references to Mr. Menendez’s decisions to stay in luxury hotels with a female friend.

“It’s not going to be a tabloid trial, and I am not going to let you just swish and swash nonsensical, you know, nonsensical substances — not substances, scenarios that really don’t even make for a good pulp fiction story,” he told a prosecutor at one point.

Gerald Krovatin, a lawyer in Newark who had worked with Mr. Menendez’s defense team, said, “They had really criminalized a relationship. It had its seamy underside with the Melgen girlfriends, but it was kind of a bloodless case once Walls cut all the sex out of it.”

Federal prosecutors saw their case slip away. The jury was deadlocked. Judge Walls tried to encourage them to reach a decision but eventually had to declare a mistrial. Jurors told reporters afterward that they were stuck at 10-2, with the majority for acquittal. It was unusual insight into a jury’s deliberations, and it made some in the Justice Department unenthusiastic about a retrial.

The government was facing other hurdles.

Judge Walls had said he would not preside over the retrial, and the case was expected to go to Judge William Martini. Reflecting Mr. Menendez’s long and deep involvement in New Jersey politics, every other federal judge in Newark, where the first trial was held, had either passed or recused from the case; Mr. Menendez had helped sponsor several of the judges for the bench.

Prosecutors had reason to feel anxious about arguing the case before Judge Martini. A former Republican congressman — he served in the House with Mr. Menendez in the mid 1990s — he has been an outspoken critic of government overreach.

In one of the most prominent public corruption cases in the state’s recent history, against Mayor Sharpe James of Newark, Judge Martini questioned whether Mr. James’ actions had been different than what any public official would do for a constituent. He sentenced Mr. James to 27 months in prison, a fraction of what the United States attorney in the case — a future governor named Chris Christie — had sought.

Judge Martini, too, decided to pass on the case, which would have moved it to Trenton. That move might have hurt the defense, because the pool of jurors would be less likely to include many Hispanics, who were considered sympathetic to Mr. Menendez.

The government risked looking small with a case based primarily on a relatively lesser offense, of failing to report trips and gifts on disclosure forms. There was the chance that jurors would have decided the matter was not a federal crime, and better handled by the Senate Ethics Committee, which is conducting its own investigation.

“I don’t know if they thought ‘small potatoes,’ or they would look petty if all they got out of it was a false statements case,” Mr. Krovatin said. “You have to believe that Judge Walls’s decision became the tipping point in terms of their decision.”

“He knocks out the six-figure dollars in the case,” Mr. Krovatin said. “They must have thought that made a retrial much more consistent with the defense theme of friendship and exchange of mutual friendship-related favors as opposed to bribery.”

After the years of headlines, months of courtroom battles and the surprising decision to retry the case, federal prosecutors delivered the news to the defense that they were abandoning all the charges almost casually.

Peter Koski, the lead prosecutor, walked over to the defense table during a procedural hearing, out of earshot of the judge, leaned over and said they were dropping the case.

As much as Judge Walls’s decision to toss some of the charges hobbled the prosecution, it also handed the government a public relations lifeline.

“The case got harder, and the politics of dismissing it got a lot easier,” said Matt Miller, a former spokesman for the Department of Justice who also closely counseled Mr. Menendez during the investigation. “Now you have a graceful exit.”