President Trump abused his authority by ordering the denial of federal funds to San Francisco and other sanctuary cities for refusing to cooperate with immigration officers, a federal appeals court ruled Wednesday.

In a 2-1 decision, the Ninth U.S. Circuit Court of Appeals in San Francisco said Trump acted unconstitutionally in an executive order he issued in January 2017, five days after taking office, declaring that local governments that refused to provide information or access to federal immigration agents would be ineligible for federal grants. Federal courts since then have refused to enforce the order or other administration efforts to withhold funding from sanctuary cities.

“The United States Constitution exclusively grants the power of the purse to Congress, not the president,” Chief Judge Sidney Thomas said in the majority opinion. Because Congress has not passed a law authorizing the president to deny funding to sanctuary cities — and, in fact, rejected such legislation even during President Barack Obama’s administration — Trump’s order “violates the constitutional principle of the separation of powers,” Thomas said.

The appeals court upheld an injunction by U.S. District Judge William Orrick of San Francisco in November that prohibited Trump from withholding federal grants from San Francisco and Santa Clara County, which jointly sued to challenge the executive order. Both counties receive more than $1 billion a year in federal funds and said they would be severely harmed by a cutoff.

The court rejected provisions of the injunction that barred funding cutoffs to any of the hundreds of sanctuary cities across the country. Thomas said the court lacked evidence on the potential impact of the cuts in other states, and told Orrick to reconsider the scope of his order.

However, federal judges in other states have issued similar injunctions prohibiting cutoffs of federal funding. Additionally, a federal judge in Sacramento has rejected the Trump administration’s challenge to a “sanctuary state” law that restricted the authority of law enforcement agencies in California to share information with immigration officers.

And federal courts in San Francisco and elsewhere have not been receptive to Attorney General Jeff Sessions’ attempt to craft a narrower order that would deny specific grants to cities and counties that refused to notify immigration agents of the impending release of undocumented immigrants in local custody.

In defense of Trump’s order, Justice Department lawyers argued that the president never sought to cut off all federal funding to sanctuary cities, but simply directed Sessions to enforce existing laws. One such law requires state and local governments to allow police to share information with federal officers about the immigration status of anyone in local custody.

San Francisco officials say the city provides immigration-status information when the government asks for it, but refuses to go further by notifying federal agents of immigrants’ scheduled release dates, or obeying federal detainers to keep inmates in custody beyond their release dates so that immigration agents can pick them up. Sessions contends federal law requires local governments to comply with detainers, but the courts so far have not agreed.

Dissenting from Wednesday’s ruling, Judge Ferdinand Fernandez said Trump had merely directed his subordinates “to see that duly adopted laws were faithfully executed.” Fernandez accused Orrick and the appeals court majority of rewriting Trump’s order and then choosing to “take up arms to vanquish the imagined beast.”

But Thomas, joined by Judge Ronald Gould, said Trump had made his intentions clear in the language of his order, which declared that cities and counties refusing to comply with immigration law, as the administration interpreted it, were “not eligible to receive federal grants,” except those the administration considered necessary for law enforcement purposes.

Thomas also cited a White House press release that said Trump had issued an order “halting federal funding for sanctuary cities.”

Justice Department spokesman Devin O’Malley said the ruling was “a victory for criminal aliens in California, who can continue to commit crimes knowing that the state’s leadership will protect them from federal immigration officers.” He did not say whether the Trump administration would appeal.

San Francisco City Attorney Dennis Herrera called the ruling “a victory for the rule of law.”

“The only way to stop a bully is to stand up to him,” Herrera said in a statement. He also said San Francisco’s “sanctuary policies make our city safer by encouraging anyone who has been a victim or witness to a crime to tell police” without fear of being deported.

Bob Egelko is a San Francisco Chronicle staff writer. Email: begelko@sfchronicle.com Twitter: @BobEgelko