The federal government has six months to identify potentially thousands of children who were separated from their families at the US-Mexico border early in Donald Trump’s term, a judge said on Thursday.

Trump administration officials said they had a goal of six months but opposed any deadline, saying it could take as long as two years to reunite children with their parents if efforts to speed up the process fail.

The US district judge Dana Sabraw said he would be willing to consider an extension past 25 October but that he wanted to establish a firm date.

“I am going to issue an order to do this in six months, subject to good cause,” Sabraw said. “It is important for all government actors to have a timeframe, a deadline.”

The administration will review about 47,000 cases of unaccompanied children in custody between 1 July 2017 and 25 June 2018 – the day before Sabraw halted the general practice of separating families and ordered that children under government care at the time be reunited in 30 days.

More than 2,700 children had been separated when Sabraw issued his June order and they have largely been reunited. Then, in January, the internal watchdog for the US health and human services department (HHS) reported that thousands more children may have been split since summer 2017. The department’s inspector general said the precise number was unknown.

Jonathan White, a commander of the US public health service and the HHS’s point person on family reunification, testified for about an hour on how he would oversee the effort. After he spoke, the judge praised his continued involvement, calling him “a beacon of light”.

The administration will develop a statistical model within 12 weeks to search for factors most likely for separations. Those factors, detailed in an earlier court filing, include children under five, younger children traveling without a sibling and those detained in the border patrol’s El Paso, Texas, sector, where the administration ran a trial program that involved separating nearly 300 family members from July to November 2017.

On a parallel track, the administration will begin work immediately on identifying children who were separated after US customs and border protection introduced a tracking system in April 2018.

The government lacked tracking systems when the administration began a “zero-tolerance” policy a year ago to criminally prosecute every adult who entered the country illegally from Mexico, sparking an international outcry when parents couldn’t find their children.

Poor tracking before April 2018 and the fact that still-separated children are no longer in US custody complicates the latest task.

The vast majority of separated children are released to relatives, but many are not parents. Of children released in the 2017 fiscal year, 49% went to parents, 41% to close relatives such as an aunt, uncle, grandparent or adult sibling and 10% to distant relatives, family friends and others.

White told the judge that six months was his “operational target” but that he had been wrong before and there were still lots of unknowns about the process.

The American Civil Liberties Union, which sued over family separations, wanted the job done in three months, which White said was unrealistic. The ACLU agreed on Thursday to six months.

“This order shows that the court continues to recognize the gravity of this situation,” the ACLU attorney Lee Gelernt said after the hearing.

The Associated Press and Reuters contributed to this report