The Trump administration’s plan to deport more than 300,000 undocumented immigrants whose home countries have been hit by disasters was blocked Wednesday by a San Francisco federal judge, who said the administration had abruptly changed federal policies without explanation and may have been motivated by racism.

U.S. District Judge Edward Chen issued a nationwide injunction preserving temporary protected status for former residents of El Salvador, Nicaragua, Haiti and Sudan. President Trump’s Department of Homeland Security had decided to revoke that status for all four nations, eliminating the immigrants’ protections from deportation. The deportations had been scheduled to start with the removals of about 1,000 Sudanese Nov. 2 and proceed through fall 2019.

Temporary protected status, established by a 1990 federal law, allows undocumented immigrants with no serious criminal records to live and work in the United States if a natural disaster or war in their homeland has made it unsafe to return. The status is typically renewed every 18 months.

Immigrants from 10 nations currently hold the status. The suit was filed on behalf of 263,000 Salvadorans, who were allowed to remain in the U.S. after a 2001 earthquake; 5,300 Nicaraguans, after a hurricane in 1998; 46,000 Haitians, following a 2010 quake; and 1,000 Sudanese because of an ongoing civil war.

The program hit the public spotlight after Trump, discussing temporary protected status at a White House meeting in January, asked why the U.S. was admitting so many people from what he called “s—-hole countries,” referring to Haiti and African nations.

A week later, Homeland Security announced the termination of protected status for Haitians.

That was evidence, Chen said, that the decision may have been influenced by racial discrimination, in violation of the Constitution.

He also cited Trump’s campaign claim that Mexican immigrants were drug dealers and rapists as well as his assertions last year that Haitian immigrants “all have AIDS” and that Nigerians, after entering the United States, would “never go back to their huts” in Africa.

In addition, Chen noted, then-Homeland Security Secretary Elaine Duke said in a memo last year that revocation of protected status was “a result of an America First view,” one of Trump’s frequent slogans. That and other writings suggest that Duke was “largely carrying out or conforming with a predetermined presidential agenda to end TPS,” the judge said.

He said advocates for the immigrants were likely to prove that the administration had violated a federal law requiring the government to present a rational explanation for policy changes that cause hardship to individuals.

Past administrations have renewed protected status due to ongoing hardships in the immigrants’ homelands, such as a hurricane and an outbreak of cholera in Haiti and violence and natural disasters in Nicaragua and El Salvador. But Trump’s Homeland Security Department said the law required termination of protected status once the hardships caused by the original disaster no longer exist.

While Justice Department lawyers argued that the administration was still following the same law and was merely changing its “emphasis,” Chen said its new policy was “a clear departure from prior (government) practice,” carried out without any explanation.

If the change is carried out, he said, holders of protected status —some of whom have lived in the U.S. for 20 years — “risk being uprooted from their homes, jobs, careers, and communities. They face removal to countries to which their children and family members may have little or no ties and which may not be safe,” and their U.S.-born children would face the choice of leaving their native country or separating from their parents.

It’s possible the Trump administration will appeal the ruling.

Representatives of the Department of Homeland Security said it would not comment on pending litigation. Department of Justice officials did not immediately respond to a request for comment.

Bob Egelko is a San Francisco Chronicle staff writer. Email: begelko@sfchronicle.com Twitter: @BobEgelko