A federal judge in California on Tuesday ordered the U.S. Border Patrol to stop separating families at the U.S.-Mexico border and to reunite families already separated within 30 days.

Any children younger than 5 must be reunited within 14 days of Tuesday's ruling, U.S. District Judge Dana Sabraw in San Diego ruled.

Sabraw, an appointee of President George W. Bush, also required the government to provide phone contact between parents and their children within 10 days.

“The facts set forth before the court portray reactive governance responses to address a chaotic circumstance of the government’s own making,” Sabraw wrote. “They belie measured and ordered governance, which is central to the concept of due process enshrined in our Constitution.”

More than 2,000 children have been separated from their parents in recent weeks and placed in government-contracted shelters -- hundreds of miles away, in some cases -- under a now-abandoned policy toward families caught illegally entering the U.S.

Amid an international outcry, President Donald Trump last week issued an executive order to stop the separation of families and said parents and children will instead be detained together.

The lawsuit in San Diego involves a 7-year-old girl who was separated from her Congolese mother and a 14-year-old boy who was separated from his Brazilian mother.

Also Tuesday, 17 states, including New York and California, sued the Trump administration to force it to reunite children and parents. The states, all led by Democratic attorneys general, joined Washington, D.C., in filing the lawsuit in federal court in Seattle, arguing that they were being forced to shoulder increased child welfare, education and social services costs.

In a speech before the conservative Criminal Justice Legal Foundation in Los Angeles, U.S. Attorney General Jeff Sessions defended the administration for taking a hardline stand on illegal immigration and said the voters elected Trump to do just that.

Juan Sanchez, chief executive of the nation's largest shelters for migrant children, said he fears a lack of urgency by the U.S. government could mean it will take months to reunite families.

Sanchez with the nonprofit Southwest Key Programs told the Associated Press that the government has no process in place to speed the return of children to their parents.

"It could take days," he said. "Or it could take a month, two months, six or even nine. I just don't know."

Health and Human Services Secretary Alex Azar told Congress on Tuesday that his department still had custody of 2,047 immigrant children separated from their parents at the border. That is only six fewer children than the number in HHS custody as of last Wednesday.

Tens of thousands of Central American migrants traveling with children -- as well as children traveling alone -- are caught at the U.S.-Mexico border each year. Many are fleeing gang violence in their home countries.

At a Texas detention facility, immigrant advocates complained that parents have gotten busy signals or no answers from a 1-800 number provided by federal authorities to get information about their children.

Many children in shelters in southern Texas have not had contact with their parents, though some have reported being allowed to speak with them in recent days, said Meghan Johnson Perez, director of the Children's Project for the South Texas Pro Bono Asylum Representation Project, which provides free legal services to minors.

Since calling for an end to the separations, administration officials have been casting about for detention space for migrants, with the Pentagon drawing up plans to hold as many as 20,000 at U.S. military bases.

The Associated Press contributed to this report.