The Trump administration can withhold grant money from New York City and several states that don’t give US Immigration and Customs Enforcement access to jails, an appeals court ruled Wednesday.

The decision, a serious blow to “sanctuary cities,” reversed a previous ruling that prevented the federal government from withholding grants from the Big Apple and seven states — New York, New Jersey, Connecticut, Washington, Rhode Island, Massachusetts and Virginia.

The US Justice Department in a statement praised the decision, calling it a “major victory for Americans.”

“Today’s decision rightfully recognizes the lawful authority of the [US] attorney general to ensure that Department of Justice grant recipients are not at the same time thwarting federal law-enforcement priorities,” a DOJ spokesperson said.

The grant money — which comes from the Edward Byrne Memorial Justice Assistance Grant — is doled out to local governments to assist with law enforcement.

The program is named after slain NYPD officer Edward Byrne, who was killed while protecting a Guyanese immigrant’s Queens house. The ruling came on the 31st anniversary of Byrne’s slaying on Feb. 26, 1988.

In 2018, then-Acting New York Attorney General Barbara Underwood and five other AGs filed suit, challenging the administration’s plan to claw back some $9 million disbursed under the grant in 2017.

“The case implicates several of the most divisive issues confronting our country,” Second Circuit Court of Appeals Judge Reena Raggi wrote in the three-judge panel’s decision that she said addressed the “ability of states and localities to adopt policies on such matters contrary to, or at odds with, those of the federal government.”

A spokesperson for Underwood’s successor, AG Letitia James, said her office hasn’t decided whether it will appeal.

“We’re still reviewing the decision and determining what our options are,” the rep said.

Mayor Bill de Blasio pledged a legal challenge.

Trump and DOJ officials have repeatedly railed against sanctuary cities, which block federal immigration-enforcement officers from jails and do not alert federal authorities when the cities release illegal immigrants.

Additional reporting by Julia Marsh