Somerville will become the first city in Massachusetts to bar city police from detaining immigrants simply because they are suspected of living in the U.S. illegally.

Mayor Joseph Curtatone will sign an executive order Thursday that says city police will only hold someone for U.S. Immigration and Customs Enforcement (ICE) if the agency has a criminal warrant or if there is a “legitimate law enforcement purpose” beyond immigration status.

Curtatone says the federal Secure Communities program — which allows ICE to ask local authorities to hold detainees for up to 48 hours after the person posts bail or is ordered released — has compromised public safety in Somerville.

“This executive order will free up our law enforcement officials and place our trust back in them to go after real, legitimate threats to our community’s safety, find them, arrest them and prosecute them to the full extent of the law,” Curtatone said in a statement released after a press conference announcing the order.

He says Secure Communities has been a failure because more than half of those people deported from Massachusetts in the past two years had no criminal convictions — separating families and discouraging cooperation with police and the reporting of crimes.

“It has instilled an incredible amount of fear from people that they’ll be torn away from their families, from their communities,” Curtatone told WBUR on Tuesday. “And it has discouraged them to help the community solve issues around public safety.”

In a responding statement, ICE said:

U.S. Immigration and Customs Enforcement will continue to work cooperatively with law-enforcement partners as the agency seeks to enforce its priorities through the identification and removal of convicted criminals and others who are public-safety threats.

Jessica Vaughan, director of policy studies at the Center for Immigration Studies, says Secure Communities' critics have gotten a "misguided impression" of the program.

Vaughan criticized Curtatone's order Wednesday.

"The mayor is really injecting his political views in a public safety matter in a way that could have unfortunate consequences for the city of Somerville and the people who could become the future victims of these individuals," she said.

Lawmakers are considering a bill that would remove the state from the Secure Communities program, but that legislation remains in limbo.

With reporting by WBUR's Asma Khalid and Benjamin Swasey