President Donald Trump has ordered the withdrawal of US military forces from Somalia, in a move that comes two just weeks after he had told the Pentagon to reduce the American troop presence in Iraq and Afghanistan.

The Pentagon said Mr Trump ordered the withdrawal by early 2021, but stressed the move did not mean the US was disengaging from Africa.

A US defence official said there were roughly 700 US troops in Somalia, where they have been helping the country fight al-Shabaab, a terrorist organisation that has affiliations with al-Qaeda.

“We remain committed to our African partners and enduring support through a whole-of-government approach,” the Pentagon said in a statement on Friday. “We will continue to degrade violent extremist organisations that could threaten our homeland while ensuring we maintain our strategic advantage in great power competition.”

In the wake of his loss to Joe Biden in the US election, Mr Trump fired Mark Esper, his defence secretary, partly because he wanted to reduce opposition inside the Pentagon to reducing US troop levels in Iraq and Afghanistan. He replaced Mr Esper with Chris Miller, acting defence secretary, who pushed through the drawdown order.

Mr Trump has long said the US should not have so many troops overseas, particularly in combat zones. He has even threatened to remove troops from allies such as Japan and South Korea, partly for cost reasons.

He has faced repeated criticism during his four years for threatening to remove troops from other countries. Jim Mattis, his first defence secretary, quit two years ago after Mr Trump abruptly ordered a withdrawal from Syria.

Congressional negotiators this week moved to prevent Mr Trump from following through on a plan to withdraw roughly one-third of the 34,500 US troops stationed in Germany.

The restriction was included in a $740bn defence bill that is expected to pass Congress. Mr Trump has vowed to veto the bill over others issues — including legal protections for social media companies — but lawmakers could override that veto with a two-thirds majority in both the House and Senate.

Lawmakers wrote in the legislation that the presence of US forces in Germany was a “strong deterrent” to Russian aggression and that cutting US troop levels would hurt national security and weaken the Nato alliance.

“Reducing the number of members of the US armed forces in Germany during a time of growing threats in Europe would constitute a grave strategic mistake,” lawmakers said in the bill.

Follow Demetri Sevastopulo on Twitter