A federal judge tossed a lawsuit Thursday that would have pushed the State Department and FBI to do more to try to track down Hillary Clinton’s emails, ruling the government has done all it reasonably could to locate the former secretary of state’s messages.

Two watchdog groups, Judicial Watch and Cause of Action, had sued in 2015 demanding the government recover all of Mrs. Clinton’s emails, saying she violated open-records laws by not preserving her messages.

U.S. District Judge James E. Boasberg, though, said the FBI did what it could, and did manage to recover thousands of messages Mrs. Clinton didn’t return herself.

“Those efforts went well beyond the mine-run search for missing federal records … and were largely successful, save for some emails sent during a two-month stretch. Even then, the FBI pursued every imaginable avenue to recover the missing emails,” wrote Judge Boasberg, an Obama appointee to the court.

Originally, the case had been dismissed as moot, but last year the U.S. Circuit Court of Appeals for the District of Columbia reversed that finding, ordering the government to “shake loose a few more emails.”

But Judge Boasberg put an end to the shaking Thursday.

“The Court of Appeals may have asked the Government to ‘shak[e] the tree harder’ for more emails, but it never suggested that the FBI must shake every tree in every forest, without knowing whether they are fruit trees,” he wrote.

The judge said the FBI had already uncovered 55,000 pages of emails when it was ordered to do more. The agency interviewed people that most frequently exchanged work related emails with Mrs. Clinton, contacted her four service providers, and reviewed two phones and three iPads, which yielded little records.

Tom Fitton, president of Judicial Watch, said he had expected a better effort out of the Trump administration in tracking down Mrs. Clinton’s emails.

“I think the president, once he learns about this, is going to blow his top and he ought to,” Mr. Fitton said.

John Vecchione, president of Cause of Action, said this does not prove that all of Mrs. Clinton’s “unlawfully removed email records” were recovered.

“Unfortunately, the court concluded that efforts by the FBI in its investigation of Secretary Clinton’s handling of classified material, which resulted in the recovery of numerous emails that Clinton had not previously turned over, left nothing further for the attorney general to do,” he said.