A federal judge in San Francisco blocked the Trump administration from ending special protections for immigrants from four countries devastated by war and natural disaster, temporarily relieving more than 300,000 people from the threat of deportation.

Immigrants from El Salvador, Haiti, Nicaragua and Sudan — some of whom have spent two decades in the United States — cheered Wednesday’s decision to retain the program, which was signed into law in 1990 by President George Bush. The program includes more than 263,000 Salvadorans, almost 59,000 Haitians, more than 5,000 Nicaraguans and more than 1,000 Sudanese, according to the court.

The judge, Edward M. Chen, did not rule on the merits of the case. But in the injunction, he said the beneficiaries, if deported, “indisputably will suffer irreparable harm and great hardship,” many having to choose between bringing their American-born children with them or splitting their families apart.

The injunction was another instance of a judge using President Trump’s own words on immigration against him. Judge Chen cited comments in the president’s speeches and Twitter posts as evidence of an “animus against nonwhite, non-European immigrants.” He wrote that there was “circumstantial evidence of race being a motivating factor.”