Major television networks and Facebook have taken the unusual step of rejecting an inflammatory ad by President Trump’s political team that effectively closes a fiercely fought midterm campaign with a message portraying immigrants as a violent threat.

The 30-second political spot, which mirrors the president’s apocalyptic warnings about a caravan of asylum seekers in Mexico, was widely denounced as racist and misleading after Mr. Trump shared a longer version of it last week.

NBC and Facebook, both of which had run the ad over the weekend, reversed course after a backlash on Monday and announced that the commercial would be removed, saying it fell short of their in-house advertising standards. CNN had refused to air the ad from the start, calling it “racist.”

Even Fox News, which has made warnings about the migrant caravan a staple of its prime time coverage of the midterms, felt compelled to distance itself from the ad. The network’s president of ad sales, Marianne Gambelli, said in a statement that the network had stopped airing it on Sunday. “It will not appear on either Fox News Channel or Fox Business Network,” she wrote.