A Home Office policy of removing EU citizens found sleeping rough on UK streets is unlawful and must stop, the High Court has ruled.
A judge said the measure, introduced last year, was discriminatory and broke freedom of movement rules.
Campaigners brought the case on behalf of three men facing removal.
The government said it was disappointed by the ruling - which applies to people from the EU and European Economic Area - but would not be appealing.
The Public Interest Law Unit (PILU) at Lambeth Law Centre, which took out the judicial review, said the decision would affect hundreds of people.
It said the Home Office had been carrying out "regular raids" on locations where officials believed they would find European nationals who could be deported.
In her ruling, the judge, Mrs Justice Lang, also said the Home Office was wrong to have used the raids as a chance to verify whether the rough sleepers were abusing their right to reside in another European nation.
PILU said the High Court had shown itself willing to protect the rights of a vulnerable group, adding: "Homelessness cannot humanely be dealt with by detaining or forcibly removing homeless people."
Three men facing removal orders from the Home Office were selected as "test cases" for the hearing:
- Latvian Gunars Gureckis, who had been in the UK since 2009, and had a long history of sleeping rough interspersed with periods where he did have accommodation
- Polish citizen Mariusz Cielecki, who arrived in the UK in 2015 and unsuccessfully tried to appeal against his order, claiming he was looking for work
- Polish citizen Mariusz Perlinski, who was in poor health and alcohol dependent and had lived in the UK since 2011. He became homeless in November 2016 and was held in an immigration centre.
The judge said the order should be dropped against Mr Gureckis, while Mr Cielecki is now expected to appeal against his. The order against Mr Perlinski was withdrawn in November after he began living with a relative.
She said "rough sleeping, even accompanied by low level offending such as begging, drinking in a public place and other street nuisances, would not be grounds for removal" and the Home Office's "less favourable" treatment of rough sleepers from outside the UK could not be justified.
The Home Office said the EU's Free Movement Directive allowed member states to impose restrictions on people in certain situations, including where there were concerns about security, public health, or fraud.
Its lawyers had argued the operations to remove the rough sleepers were a "sensible and lawful approach".
A spokesman said: "We will consider carefully what steps are necessary to ensure we reflect the judgment in future enforcement."
He added that most of the people removed under the measure had not exercised their rights to residency in the UK when required and were therefore unlawfully in the country.
Speaking after the ruling, Matthew Downie from the homeless charity Crisis, who gave evidence against the Home Office, said the policy had been "brutal and indiscriminate". He said in many cases people had been taken away from help they were getting to resolve their homelessness.