A federal judge in Texas has temporarily blocked a severe state immigration law that was supposed to take effect on Friday and would have outlawed sanctuary cities and penalised local officials who do not cooperate with federal deportation efforts.

The decision on Wednesday by US District Judge Orlando Garcia delivers a temporary blow to the state's campaign - backed by the Trump administration - to compel localities to detain immigrants so federal agents can take them into custody.

In a 94-page decision, Garcia wrote that parts of the Texas law are likely to be found unconstitutional. He enjoined the state from punishing local officials, infringing on their right to free speech and forcing them to detain immigrants for federal immigration officials, an act that is currently voluntary. And he wrote that cities and towns had provided “overwhelming” and “ample” evidence that cooperating with immigration officials will “erode public trust and make many communities and neighbourhoods less safe” as well as harm the state economically.

“Indeed, at the end of the day, the Legislature is free to ignore the pleas of city and county officials, along with local police departments, who are in the trenches,” Garcia wrote. “The Court cannot and does not second guess the Legislature. However, the State may not exercise its authority in a manner that violates the United States Constitution.”

Governor Greg Abbott (Republican) and Texas Attorney General Ken Paxton (Republican) vowed to appeal. “Today's decision makes Texas' communities less safe,” Abbott said in a statement.

The law would fine local officials who refused to assist federal agencies in working to deport illegal immigrants up to $25,500 a day. Elected and appointed officials could lose their jobs, and police and sheriffs could go to jail for up to a year on misdemeanour charges.

“The Court was right to strike down the guts of a law that is not only patently unconstitutional but bad policy as well,” said Lee Gelernt, an American Civil Liberties Union lawyer who argued against the case in court on behalf of several Texas cities and towns. “Hopefully other states will now not follow Texas' lead.”

Garcia's decision comes as Texas, home to an estimated 1.6 million undocumented immigrants, is grappling with historic flooding and dozens of deaths following Hurricane Harvey. It also lands amid growing fears that the Trump administration will end an Obama-era programme that has granted nearly 800,000 immigrants brought to the United States as children reprieves from deportation.

Texas is one of 10 states that have threatened to sue the Trump administration if it does not begin to phase out the deportation-relief program by next week.

Abbott said the Texas statute would increase public safety by preventing local law enforcement from releasing foreign-born residents from jails or prisons before immigration agents can take them into custody.

The small border city of El Cenizosued to stop the law, and was soon joined by Houston, San Antonio, Dallas, Austin and other cities. They said the law could lead to racial profiling, make immigrants afraid to report crime and ignite a backlash that could devastate the state economically.

The US Justice Department sided with Texas in defending the law in federal court.

President Donald Trump signed an executive order in January that threatened to withhold federal funds from sanctuary cities. A federal judge later halted that order, saying it was overly broad. The administration has continued to try to steer funding away from cities and towns that refuse to help enforce immigration law.

El Cenizo Mayor Raul Reyes said Wednesday's ruling left him in tears, thinking about the hate-filled messages he's received for filing the lawsuit, and the immigrants who will rest more easily come Friday - at least temporarily. “This is great news for thousands of families here in Texas,” he said.

Houston Mayor Sylvester Turner, who is touring flooded areas of the city, tweeted: “Happy to learn a federal judge blocked the Texas law aimed at making local police immigration enforcers. Need them for fighting local crime.”

The aftermath of Hurricane Harvey Show all 19 1 /19 The aftermath of Hurricane Harvey The aftermath of Hurricane Harvey A tattered U.S. flag damaged in Hurricane Harvey, flies in Conroe, Texas Reuters The aftermath of Hurricane Harvey Lisa Rehr holds her four-year old son Maximus, after they lost their home to Hurricane Harvey, as they await to be evacuated with their belongings from Rockport, Texas Reuters The aftermath of Hurricane Harvey People line up for food as others rest at the George R. Brown Convention Center AP Photo/LM Otero The aftermath of Hurricane Harvey Volunteers with The American Red Cross register evacuees at the George R. Brown Convention Center Reuters/Nick Oxford The aftermath of Hurricane Harvey Soldiers with the Texas Army National Guard help the residents of Cyprus Creek Reuters The aftermath of Hurricane Harvey Residents wade through floodwater Reuters/Nick Oxford The aftermath of Hurricane Harvey Residents walk along the flooded roadway of Texas 249 as they evacuate their adjacent neighborhoods EPA The aftermath of Hurricane Harvey A man floats past a truck submerged on a freeway flooded by Tropical Storm Harvey on Sunday AP The aftermath of Hurricane Harvey People are rescued by airboat as they evacuate from flood waters from Hurricane Harvey in Dickinson, Texas Reuters The aftermath of Hurricane Harvey James Archiable carries his bike through the flooded intersection at Taylor and Usenet near downtown Houston, Texas EPA The aftermath of Hurricane Harvey A massive sinkhole opened up on a motorway in Rosenburg, a city 25 miles southwest of Houston, Texas Rosenberg Police The aftermath of Hurricane Harvey People are rescued from flood waters from Hurricane Harvey in an armored police mine-resistant ambush protected vehicle in Dickinson, Texas Reuters The aftermath of Hurricane Harvey People are rescued from flood waters from Hurricane Harvey on a boat in Dickinson, Texas Reuters The aftermath of Hurricane Harvey Evacuees are airlifted in a US Coast Guard helicopter after flooding due to Hurricane Harvey inundated neighborhoods in Houston, Texas Reuters The aftermath of Hurricane Harvey Evacuees leave a US Coast Guard helicopter after being rescued from flooding due to Hurricane Harvey in Houston, Texas Reuters The aftermath of Hurricane Harvey Residents look on at a submerged motorway during a break in the rain in Houston, Texas EPA The aftermath of Hurricane Harvey People photograph the submerged motorway interchange EPA The aftermath of Hurricane Harvey Debris lies on the ground after a building was destroyed by Hurricane Harvey in Aransas Pass, Texas AP The aftermath of Hurricane Harvey Dominic Dominguez searches for his boat in a boat storage facility that was heavily damaged by Hurricane Harvey near Rockport, Texas EPA

Few localities in Texas actually declare themselves “sanctuaries” for undocumented immigrants, though many, including Houston, say they are “welcoming” cities and limit how frequently they will cooperate with federal immigration officials.

For instance, some are willing to hand over violent criminals, but unwilling to turn over someone arrested for a minor offence.