New York Gov. Andrew Cuomo sent a “cease and desist” letter to federal immigration authorities Wednesday, ordering them to alert state and local police before they attempt any more arrests and demanding they leave some illegal immigrants alone altogether.

He also said he was issuing an executive order banning ICE officers from making arrests at state prisons and jails, and ordered agencies not to ask about immigration status. He was hoping to deny federal authorities information that could lead to future deportations.

“I think it’s time a state stood up to ICE and made this case and let them know that people have rights,” the governor said, accusing the federal government of “stoking terror among law-abiding residents.”

His move puts New York back in stiff competition with California in the battle for the title of the country’s premier sanctuary state — but it drew a stiff rebuke from the Homeland Security Department and Mr. Homan himself, who said as a native New Yorker he was insulted by the governor’s attack on him and his people.

“ICE will continue to protect New York communities against public safety and national security threats and it is false and offensive for the governor to say otherwise,” he said.

Mr. Cuomo’s letter is part of a further leftward drift for the governor, who is facing a surprisingly strong challenge from actress Cynthia Nixon in this year’s Democratic primary. Ms. Nixon this month vowed to make New York a “real sanctuary state” and wants to grant illegal immigrants privileges such as driver’s licenses.

Mr. Cuomo, though, didn’t mention politics. Instead, he said he was responding to “lawlessness” in recent targeted enforcement sweeps by ICE officers. He said one Staten Island community was terrified by officers who went after targets and found they weren’t home, but arrested other illegal immigrants at the residences.

His brazen letter also accused ICE of targeting illegal immigrants who are outspoken advocates for the unauthorized population, singling them out for deportation in order to squelch dissenting voices.

That appeared to be a reference to Ravi Ragbir, a one-time legal immigrant from Indonesia who was convicted of loan fraud and ordered deported. The Obama administration had let him remain in the U.S. and he became an immigrant-rights activist.

The Trump administration this year told him his Obama-era deferrals would not be extended — but a federal judge stepped in and blocked his deportation, ruling he and other illegal immigrants had a “right to say goodbye” to their families.

Mr. Cuomo also took umbrage at ICE’s arrest of a farmworker, Marcial DeLeon-Aguilar, earlier this month. The governor said officers didn’t announce themselves and were rude to the farmer who was employing DeLeon-Aguilar.

DeLeon-Aguilar has been deported three times before and has a reckless aggravated assault conviction on his record, Mr. Homan countered.

“In each targeted enforcement action in which these specific individuals were sought, ICE officers acted professionally and within their legal authorities under federal immigration law,” he said.

Activists cheered Mr. Cuomo’s move but said more is needed. They said Mr. Cuomo’s ban on ICE officers in state buildings doesn’t apply to the courts, meaning ICE is still able to arrest illegal immigrants and other criminal aliens inside of courthouses.

Security analysts, though, said kicking ICE out of state facilities and limiting cooperation would mean the release of more criminals into communities.

Last week, ICE announced a major targeted sweep that netted 225 people in the New York City area. Of those, 180 had criminal records, the government said. More than 60 of them had been in state or local police custody while they were being sought by ICE but were released under sanctuary policies.

Mr. Homan said sanctuary policies like Mr. Cuomo is promoting actually cause more conflicts and spread more fear in communities. He said if states and localities would allow ICE into their prisons and jails to arrest criminals, officers wouldn’t have to go out into communities searching for their targets.

Those sweeps sometimes end up netting illegal immigrants who weren’t even targets — though they are in the country illegally, and are liable for deportation, ICE says.

Mr. Cuomo warned of a new slew of lawsuits against ICE if it doesn’t change.

The state has sued the Trump administration over several federal immigration-related policies, and it’s not clear what other levers Mr. Cuomo could pull to force ICE officers to stop arrests or divulge details ahead of enforcement actions.

But the federal government might have its own ways to retaliate.

Attorney General Jeff Sessions sued California last month over that state’s three new sanctuary laws, including one that limited information-sharing and another that restricted ICE access to state facilities.