President Donald Trump has ordered almost all American forces to leave Somalia as part of a worldwide withdrawal by the incumbent commander in chief before he exits the White House next month.

He will also pull troops from Iraq and Afghanistan.

The U.S. has around 700 military personnel in Somalia helping locals fight al-Shabab and Islamic State militants. Pentagon officials said some of the soldiers would be transferred to neighboring nations - allowing for cross-border operations.

The U.S. mission in Somalia has been viewed as a cornerstone of the Defense department's efforts to contain al-Qaida.

Joint Chiefs of Staff chairperson Gen. Mark Milley said last week the future layout of the U.S. military presence in Somalia was still in contention.

Milley gave no specifics but said the U.S. remained worried about the danger posed by al-Shabab, which he described as "an extension of al-Qaida," the extremist organization behind the Sept. 11, 2001, terror attacks on the U.S.

The U.S. has withdrawn from Somalia's cities of Galkayo and Bossaso. As of November, U.S. forces were still in the capital Mogadishu, Baledogle air base in the Lower Shabelle region and the port city of Kismayo.

Trump has been calling for American soldiers to come home and has denounced the country's military interference for being useless and costly.

The troop pullback reverses the policy of former U.S. defense secretary Mark Esper, who was dismissed.

Representative Jim Langevin criticized the withdrawal from Somalia - calling it as "surrender" to al-Qaida and "a gift" of China." Langevin is chairperson of the House Armed Services Committee's Intelligence and Emerging Threats and Capabilities Subcommittee.

A congressional aide, who asked not to be named, said the troop pullout comes at a difficult moment for Somalia, ahead of both parliamentary and presidential polls, and will present an early problem for president-elect Joe Biden.

Somalia has suffered years of political insecurity but, in the last few years, a peacekeeping force from the African Union along with American forces have retaken control of the country's capital and other areas from al-Shabab.

The Pentagon said the pullout doesn't signify the end of U.S. counterterror efforts in the country. "The U.S. will retain the capability to conduct targeted counterterrorism operations in Somalia," CBS News quoted the Pentagon as saying.