ON A recent afternoon excavators and dump trucks crammed into what remained of the alleyways of Xupu on the western edge of Shanghai. Their task was to remove debris from the place, formally a village but in reality an urban slum. Officials had had enough of the eyesore: in October they had sent in demolition crews which, within two months, had evicted some 13,000 migrants and flattened buildings with a total of 6,000 rooms. “Seven out of ten people I know here have left,” says Zhuang Shiguo, a 42-year-old kitchen worker from Jiangsu, a neighbouring province, standing next to a heap of rubble. In recent months the authorities have targeted more than 30 other slums in Shanghai for similar “makeovers”.

Shanghai is stepping up its efforts to control the growth of its population. One of its techniques is to make it more difficult for unskilled workers from the countryside to live there, such as by knocking down their cheap, ramshackle accommodation. Similar efforts are under way in the capital, Beijing. The governments of both cities have been deluged with complaints about pressures on transport, schools and hospitals. Their response has been to strike at those most easily displaced: rural migrants whose household-registration papers, or hukou, make them ineligible for urban benefits such as social housing or subsidised health care and education.

Last year Beijing’s government said it would not allow the capital’s population (those resident six months or longer) to exceed 23m before 2020. That is only 1m more than the current population, which inhabits an area half the size of Belgium comprising a main city, several satellite towns and a rural expanse. Shanghai followed, setting a population limit of 25m by the end of the decade for its urban and rural areas, up from 24m today. The two cities’ targets contrasted with a far more relaxed one set by Guangzhou, a megacity in the south: 18m by 2020, an increase of 4m.

Living in large cities has never been easy for rural migrants. Until 2003, police could expel anyone found without proof of employment or a residence permit (often after detaining them for weeks or even months). The hukou system is still an invisible barrier. Migrants are often not allowed to buy houses or cars in a city if they do not have hukou there. Officials, sometimes lax in their application of safety rules, are zealous in their use of them to evict migrants from lodgings.

In 2014 the central government issued a plan for what it called “new-type urbanisation”. This envisaged 60% of the population living in urban areas by 2020 (56% do now) and outlined measures to give migrants greater access to public services. But the 16 largest cities were urged to restrict migration by using a “points system” offering urban-welfare privileges only to the educated and affluent few. China’s new five-year plan for economic and social development, approved in March, calls for “people-centred urbanisation”. But some cities are allowed to be choosy about what kind of people they focus on. The government does not want the biggest ones to develop the kind of slum-sprawl that is common in other developing countries. It worries that shanty towns may breed social instability and blot the cities’ image.

Beijing and Shanghai, the megacities of greatest political and business importance, have exploited this leeway with the most enthusiasm. Both have large middle-class populations, many of whose members resent sharing the cities’ superior amenities with people they regard as outsiders.

Since 2014, the two cities have tightened restrictions on access to local schools. Parents in Beijing need to show contracts proving they have jobs and housing. This is difficult for the many migrants who are employed casually and have no formal agreement with their landlords. Such restrictions contributed to a fall in the number of migrants’ children enrolled in Beijing’s primary schools by 22% in 2014. Using similar rules, Shanghai is estimated by some Chinese scholars to have excluded 50,000-80,000 children from its primary schools in the past two years. In February the central government issued new guidelines aimed at improving the lot of more than 60m children “left behind” in the countryside by migrant workers, appealing to parents to take their children with them when possible. Beijing and Shanghai are making that more difficult.

The cities’ measures may be working. Government statistics show the migrant population in Shanghai fell by 1.5% in 2015, the first drop in 28 years. In Beijing, the number of migrants increased by 0.5%, the slowest rate since 1998.