Two women have argued in the High Court that the detention of their children over immigration issues caused them to suffer serious harm and was unlawful.
Asylum seekers Reetha Suppiah, from Malaysia, and Nigerian Sakinat Bello were arrested in February 2009.
They were taken to Yarl's Wood in Bedfordshire along with their children and spent between 12 and 17 days there.
Lawyers for the Home Secretary Theresa May said the policy of family detention was "workable and lawful".
Ms Suppiah, 37, and Ms Bello, 25, were both refused asylum and detained along with their children in UK Border Agency raids on their homes.
The families were allowed back into the community pending their legal challenges after their detention. The women are now trying to have their children's detention declared unlawful.
Rabinder Singh QC, representing both families, said the lack of safeguards to prevent minors suffering distress and trauma made Ms May's policy on detention incapable of being operated lawfully.
"Serious harm is routinely caused to detained children," he said, adding that the immigration detention system was afflicted by "fundamental structural problems".
Mr Singh said Ms Suppiah's eldest child, an 11-year-old boy, was particularly badly affected and now lived "in continuous fear of re-arrest".
Mr Singh told the High Court in London that Deputy Prime Minister Nick Clegg had described the detention of children as a "moral outrage" in Parliament in July. He expressed the view that ending it was essential to restoring "a sense of decency and liberty to the way in which we conduct ourselves".
Lawyers for Ms May said family detention was used as a last resort to remove failed asylum seekers from the UK.
Children were only held in detention as long as was necessary to achieve that result, and regard was had to the need to promote the welfare of children.
The three-day hearing continues.