They have also been a headache for security agencies, who often complained that some refugee camps and communities harboured thieves, drugs and armed militants, and that it was impossible to police a population that flowed loosely across the border and whose members often held no official IDs.
The refugee population has also become hostage to tensions between Pakistan and Afghanistan, with both countries accusing each other of harbouring militants in the porous border regions. In late 2014, when terrorists invaded a Pakistani military school, killing 141 students and teachers and enraging public opinion, authorities vowed to start sending the refugees back.
The push took many forms, from police harassment to a government publicity campaign, endorsed by officials in Kabul, that urged Afghans to return with the slogan, "My home is my flower". After refugee leaders protested, departure deadlines were postponed several times, but the trickle of returnees swelled to tens of thousands early this year, especially after the United Nations added an extra cash bonus for each family once they resettled in Afghanistan.
The surge intensified in June, when Pakistan erected a large gate at Torkham, the major border crossing near Peshawar, and announced that no Afghans could re-enter without a passport and visa. That was tantamount to social death for refugees used to visiting relatives back home and then returning to the safety and prosperity of Pakistan. Riots and shootings broke out at the border gate, but the passport policy stood.
"Torkham gate was the biggest factor. It sent out a very clear message that this was not going to be business as usual," said Imran Zeb Khan, Pakistan's chief commissioner for Afghan refugees. He said the cash incentives, as well as public encouragement from Afghan diplomats here, added to the push. By early September, more than 260,000 Afghans had been formally repatriated.
So far, most of the returnees have been undocumented refugees, those who had never registered with UN officials and lived in Pakistan illegally for years. Many were poor families without job skills and little to show for their years abroad; 70 per cent were younger than 24, and 75 per cent had been born in Pakistan. Of an estimated 1 million unregistered refugees, officials said 700,000 still remain here.
I am an old man and I can die in either country. What can I do?
One day last week, hundreds of Afghan men, women and children waited outside a government centre near Peshawar, where officials registered them as refugees for the first time and approved them for repatriation subsidies. Some said they were reluctant to leave and fearful of what awaited them. Others said they had been harassed by police and pressured for bribes to cut their waiting time.
"Last month some of my relatives went back, and the Afghan Government claimed it would provide them with rations and housing, but they are living in a tent," said Meera Jan, 89, who had waited in line for hours. "They have urged me not to leave, but the police and other officials won't let us live here anymore. I am an old man and I can die in either country. What can I do?"
Officials at the centre seemed overwhelmed, saying that the number of applicants was far higher than expected and that many were confused or had problems proving their identity. Even so, Shabbir Nawaz, a supervisor, said the centre is handling about 700 people a day. "We are trying our best, but most of them are uneducated and have no understanding of the process," he said.
Faced with a raft of complaints and a crush of applicants, the Government of Prime Minister Nawaz Sharif has already repeatedly extended the deadline for "voluntary repatriation". This month, he ordered the latest departure date postponed from the end of November to next March.
"Afghans are our brothers and very dear to us," Sharif said in a statement announcing the reprieve. "We will not allow Afghan refugees living in Pakistan to be terrified in any way. They are our guests."
Sharif's gesture was small consolation, however, for hundreds of thousands of registered refugees, especially those who own property or businesses and believed their official status was a guarantee of permanence.
Instead, they are suddenly vulnerable to financial cheating and pressure - unable to get a fair price for a car, legally barred from selling a house and worried that they will be unable to collect on debts or investments.
Khan, the commissioner for refugees, said the government is aware of such problems, as well as other family issues such as unfinished school semesters and college degrees. He said a meeting has been called with leaders of all Pakistani political parties to work out practical solutions. Individuals with special hardships, he said, are being allowed to apply for Pakistani identity documents.
Longtime refugees such as Mohammed Rauf Derrighel, 63, are fuming.
"I was a child when I came here. Now I am an old man, and suddenly I am being told to go. I feel helpless," complained the burly, grey-bearded businessman, who was commiserating with a friend at his tailor shop in Islamabad.
On the outskirts of the capital, hundreds of Afghans live in clusters of flimsy mud and straw-roofed huts, using car batteries to turn on lightbulbs and tending goats among campfires. Despite such precarious circumstances, some are registered refugees with long-standing jobs or investments in industries such as scrap metal that they now fear could be lost.