President Donald Trump has signed an executive order ending the process of separating children from families after they are detained crossing the US border illegally.

It was a dramatic turnaround for Mr Trump, who has been insisting, wrongly, that his administration had no choice but to separate families apprehended at the border because of federal law and a court decision.

The news in recent days has been dominated by searing images of children held in cages at border facilities, as well as audio recordings of young children crying for their parents.

Trump Immigration
President Donald Trump signs the executive order (Pablo Martinez Monsivais/AP)

The images sparked fury, questions of morality and concern from Republicans about a negative impact on their races in November’s midterm elections.

Until Wednesday, the president, Homeland Security Secretary Kirstjen Nielsen and other officials had repeatedly argued the only way to end the practice was for Congress to pass new legislation, while Democrats said he could do it with his signature alone.

That was what he did on Wednesday.

“We’re going to have strong, very strong borders, but we’re going to keep the families together,” said Mr Trump who said he did not like the “sight” or “feeling” of children separated from their parents.

He said his order would not end the “zero-tolerance” policy that criminally prosecutes all adults caught crossing the border illegally.

The order aims to keep families together while they are in custody, expedite their cases, and ask the Department of Defence to help house families.

Justice Department lawyers had been working to find a legal workaround for a previous class-action settlement that set policies for the treatment and release of unaccompanied children who are caught at the border.

Mr Trump’s order is likely to create a new set of problems involving length of detention of families, and may spark a fresh court fight.

INVICTUS GAMES 2017
Melania Trump had signalled her disagreement with the policy (Danny Lawson/PA)

Also playing a role in his turnaround was First lady Melania Trump.

One White House official said Mrs Trump had been making her opinion known to the president for some time that she felt he needed to do all he could to help families stay together, whether by working with Congress or acting on his own.

Ms Nielsen travelled to Capitol Hill Wednesday afternoon to brief politicians.

And members on the fence over pending immigration legislation headed to the White House to meet with Mr Trump.

Mr Trump had tweeted earlier: “It’s the Democrats fault, they won’t give us the votes needed to pass good immigration legislation.

“They want open borders, which breeds horrible crime.

“Republicans want security. But I am working on something – it never ends!”

The administration recently put into place a “zero tolerance” policy in which all unlawful border crossings are referred for prosecution, a process that moves adults to the custody of the US Marshals Service and sends many children to facilities run by the Department of Health and Human Services.

Under the Obama administration, such families were usually referred for civil deportation proceedings, not requiring separation.

The policy had led to a spike in family separations in recent weeks, with more than 2,300 minors were separated from their families at the border from May 5 through June 9, according to the Department of Homeland Security.

Two people close to Homeland Security Secretary Ms Nielsen said she was the driving force behind the plan to keep families together after they are detained crossing the border illegally.

One of the people said Ms Nielsen, who had become the face of the administration’s policy, had little faith that Congress would act to fix the separation issue and felt compelled to act.

Ms Nielsen was heckled at a restaurant on Tuesday evening and has faced protesters at her home.