A federal appeals court ruled Wednesday that a controversial government surveillance program that had collected millions of Americans’ phone records violated the law — and that claims made by FBI and other national security officials in defense of the program were not accurate.

The ruling by a three-judge panel on the U.S. Court of Appeals for the 9th Circuit will not have much immediate effect on the program it criticizes, because that record-gathering effort ended in 2015, replaced by a different method for searching phone records that was also eventually shut down.

The judges also ruled that government prosecutors must tell criminal defendants when it plans to use evidence gathered or derived from surveillance done overseas, a large, secret effort generally referred to by the executive order that authorizes such collection, 12333. It was not immediately clear how significant an impact that part of the ruling might have on the Justice Department, because the use of such material in criminal investigations has always been closely guarded within the government.

Wednesday’s ruling also stands as another judicial rebuke of intelligence officials who defended the bulk phone records program after former National Security Agency contractor Edward Snowden revealed key details of its workings in 2013.

Even as they rejected some of the government’s broader arguments, the judges unanimously upheld the convictions at the center of the case — against Basaaly Moalin and three others guilty of conspiring to send money to al-Shabab, a Somali terrorist group.

That case, and the long-running battles over privacy and security, grew from the federal government’s push to detect and prevent terrorist attacks after 9/11. Under Section 215 of the Patriot Act, the NSA gathered millions of Americans’ phone records — not the content of calls, but the records of who called whom, and for how long — to build a database that could then be searched by counterterrorism investigators.

Then Snowden shared documents that showed in greater detail how the program worked, generating fresh debate about whether the government was violating privacy rights in conducting the war on terrorism.

At the time, officials with the FBI and other intelligence agencies defended the Section 215 program as necessary to prevent attacks, and said it was instrumental to uncovering the case of the four Somali Americans who sent or conspired to send money to al-Shabab.

Then-FBI Deputy Director Sean Joyce told Congress that if not for the information from the phone-records program, the bureau “would not have been able to reopen” the investigation that led to the arrests.

After reviewing classified records, the court wrote in Wednesday’s 59-page ruling that the phone surveillance program was not so essential to the case that the convictions should be tossed out.

“To the extent public statements of government officials created a contrary impression, that impression is inconsistent with the contents of the classified record,” the judges wrote.

An FBI spokeswoman did not immediately comment. A Justice Department spokesman declined to comment.

Patrick Toomey, an attorney with the American Civil Liberties Union, said the ruling “makes clear that intelligence officials misled Congress and the public about the value of this mass surveillance program,” and called the judge’s decision “a victory for privacy rights.”

The court also rejected the Justice Department’s argument that the call records were properly obtained because they were relevant to a terrorism investigation.

That argument, they wrote, “depends on an after-the-fact determination of relevance: once the government had collected a massive amount of call records, it was able to find one that was relevant to a counterterrorism investigation.” The problem, the judges wrote, is that the Foreign Intelligence Surveillance Act “required the government to make a showing of relevance to a particular authorized investigation before collecting the records.”

Therefore, the judges found, “the telephony metadata collection program exceeded the scope of Congress’s authorization” and therefore violated the law.

The ruling marks the second time a federal appeals court has found the bulk phone records program illegal. In 2015, a federal appeals court in New York issued a scathing opinion finding the program had wrongly gathered a “staggering” volume of information about Americans to conduct “sweeping surveillance.”

That same year, Congress ended the program, replacing it with a system in which phone companies kept such records and provided information about specific numbers when presented with a court order. That replacement program, however, was deemed so difficult and unhelpful that it was effectively shelved in late 2018.