NYPD brass sent around a mayoral directive Tuesday to keep ICE agents off school grounds unless they have a “valid” warrant — one day after the US attorney general threatened to cancel funding for local governments that harbor illegal immigrants.

The move came despite Mayor de Blasio’s recent admission that Immigration and Customs Enforcement officials have never once tried to enter a city school.

An NYPD “Administrative Bulletin” sent to top brass — and obtained by The Post — says “non-local law enforcement officers” from agencies such as ICE or the FBI “will not be permitted to proceed” past the front desk without a warrant.

And even if the feds produce paperwork, cops must confer with NYPD lawyers to ensure it’s legit, according to the directive.

The memo, authorized by Chief of Department Carlos Gomez, says a precinct patrol supervisor should respond to make sure the mayor’s orders are carried out.

In addition, a commanding officer, executive officer or duty captain should be called to the scene “if there is a disagreement between a principal and non-local law enforcement officer regarding access to the school or an officer insists/demands access.”

When de Blasio last week announced his plan to keep the keep the feds out of city schools, he said: “I know it sounds outlandish, but we are seeing things we have not seen before, and there is a tremendous amount of fear out there.”

An NYPD source called the policy “ridiculous,” noting that “federal law supersedes the city.”

“We work with law enforcement. That’s our job,” the source said.

“We’re supposed to work together. .”

The head of the union that represents school safety agents said he had yet to be formally notified about de Blaiso’s directive, “probably because I would have told him the opposite of what he wants to do.”

Teamsters Local 237 President Greg Floyd said “ this comes down to the mayor posturing,” but also said he was worried his members were being placed “in a no-win situation.”

“If they try to resist ICE they may be arrested,” he said.

“The other side of the coin is if they don’t keep ICE from coming in the schools without the proper paperwork the mayor wants now, they face disciplinary action.”

On Monday, Sessions said the Justice Department would withold funding — and “claw back” cash that’ s already been doled out — from cities and states that give sanctuary to illegal aliens.

New York City agencies and non-governmental organizations received more than $40 million in grants during fiscal 2016, records show.

ICE said it currently prohibits agents from entering schools, hospitals or houses of worship “unless they have prior approval from an appropriate supervisory official or in the event of exigent circumstances.”

Department of Education spokeswoman Toya Holness called Floyd’s comments “flat-out wrong” and said that “our focus is on keeping students safe, and in response to concerns from families, we shared clear guidelines on our protocol so that schools could be prepared should a situation arise.”

Additional reporting by Shawn Cohen