Currently, the United States spends about $620 million a year for gang prevention programs and other initiatives aimed at helping support civil society in the three countries. Advocates say that cutting the funds will only accelerate the migrant flows into the United States.

But the president has repeatedly expressed anger that the United States sends money to the countries even as caravans of migrants make their way to the border.

In an email from the director of the Office of U.S. Foreign Assistance Resources on Friday night, senior State Department and United States Agency for International Development officials were instructed to “redirect” all 2018 assistance “away from these countries” and to “cease and desist” all 2017 spending already started.

The email sent State Department officials scrambling to carry out the president’s order. It was unclear how quickly the funding would cease since the governments had not yet been notified formally of the decision.

It is also unclear whether the department can redirect all of the money to other parts of the world. Some of the money meant for the 2017 fiscal year has already been allocated to specific contracts, so it might not be able to be withdrawn, according to a person familiar with the effort.

Mr. Trump told reporters that he would consider shutting down ports along the border, a decision that could imperil the transit of goods between the United States and Mexico. According to government figures, Mexico is the United States’ third-largest goods trading partner, with $557.6 billion worth of products flowing across the border in both directions.

A move by Mr. Trump to shut down or drastically curb trade with Mexico would pose significant risks to the American economy. It would also represent a stunning reversal in trade relations between the two countries, which last August put their differences aside to renegotiate their portion of the North American Free Trade Agreement. The new deal, known as the United States-Mexico-Canada Agreement, still needs to be approved by Congress.