The grandfather always feared this day would come.

In the four decades since he fled Afghanistan during the Soviet invasion, the man, Najmuddin Torjan, had been living illegally in Pakistan. He married there, had children and watched as they had children of their own. All the while, he felt the unease of making a life on borrowed land, seemingly on borrowed time.

This month, that time ran out. The Pakistani government abruptly declared that all foreign citizens living in the country without documents must leave by Nov. 1. Fearing arrest or prison, his family packed up everything: their clothes, their pots, their pans. The wooden beams from their ceiling. Their metal window frames and rusted doors.

After dismantling the place they had called home for three generations, they boarded a truck and joined a flood of Afghan migrants bound for the border.

“I tried my best in these 40 years to build a life,” said Mr. Torjan, 63, the truck parked behind him at the border. “It’s difficult. Now I’m starting again from zero.”

Mr. Torjan is one of more than 70,000 Afghans who have returned from Pakistan in recent weeks, according to the Pakistani authorities. The deportation order, which is largely seen as targeting Afghan migrants, is considered a sign of the increasing hostility between Pakistan’s government and the Taliban authorities in Afghanistan over militants operating in both countries.

In recent weeks, the 1.7 million Afghans living illegally in Pakistan have come under mounting pressure to leave, according to human rights groups and migrants. Landlords have suddenly evicted Afghan tenants, fearing large fines if they don’t. Employers have fired undocumented Afghan workers. The police have raided neighborhoods popular among Afghans, arresting those without paperwork.

Rights groups have condemned Pakistan’s actions, worried about the possibility that some Afghans could face persecution in Afghanistan for past ties to Taliban opponents.

But Pakistani officials have doubled down on the policy, declaring recently that there would be no extension of the deadline. They have established several deportation centers nationwide, signaling the government’s seriousness about detaining and repatriating Afghans.

“After Nov. 1, no compromise will be made over illegally staying immigrants,” Sarfraz Bugti, the country’s caretaker interior minister, said Thursday at a news conference in Islamabad. “Those leaving the country voluntarily would have lesser difficulties than those nabbed by the state,” he added.

With the deadline approaching, many Afghans have faced devastating decisions about whether to try to stay in a country where they are no longer welcome or to return to one where they have not lived for decades.

Those who have opted to return have flooded border crossings in recent weeks, overwhelming the authorities and aid groups. About 4,000 people are repatriating every day, more than 10 times the number before the deportation policy was announced, according to aid groups.

At the Torkham crossing in Nangarhar Province, a mountainous piece of land along Afghanistan’s border with Pakistan, trucks piled high with decades’ worth of belongings trundle across the border each day, their engines straining. Families, many hungry and tired, lie under makeshift tents as they wait to be registered by aid groups offering small stipends. Some wait for hours; others days.

Hamisha Gul, 48, sat on a metal trunk next to stacks of cotton sacks stuffed with his family’s clothes, cooking utensils and tattered schoolbooks. His two young granddaughters, their matching green dresses caked in dust, lay on two of the bags sound asleep, while his 1-year-old grandson reached for his grandmother’s arms, sobbing.

“Take the boy — my hands are hurting. I can’t hold him,” his grandmother, Zulaikha, 52, said. Mr. Gul pulled him up from her feet and sat him on his lap. The boy buried his face in his grandfather’s chest.

“He didn’t sleep at all last night; he’s too tired,” Mr. Gul, 48, explained.

His family had left Afghanistan eight years earlier under financial strain: His son, Khan Afzal Wafadar, age 15 at the time, was supporting the entire family with the less than $3 a week he was making at a brickmaking factory.

After the family moved to the Taxila town near Islamabad, the Pakistani capital, Mr. Wafadar earned five times as much doing the same work. But this month, his boss told him to either provide legal immigration documents or leave the factory. Now 23, Mr. Wafadar said he worries about finding work in Afghanistan, where joblessness has soared since the U.S.-backed government collapsed.

“There’s a Pashtun proverb: ‘If your bed belongs to another person, they have the power to take it from you in the middle of the night,’” Mr. Wafadar said. “It’s their country; they can kick us out at any time.

Nearby, at a transit center run by the International Organization for Migration, a girl named Sapna sat under the shade of an orange tarp. Like many other young people there, Sapna, 15, was born in Pakistan to Afghan parents. Now she was setting foot on Afghan soil for the first time.

As she grew up in Pakistan, her parents reminisced about the Afghanistan they remembered: the snow that blankets the capital, Kabul, in the winter. The lush mountains of the Hindu Kush. The huge lakes of bright blue water in the central valleys.

When her father said this month that the family would return, at first it felt like an adventure. The country is at peace now, he had told her, and women wear the same all-covering hijabs that Sapna did in Pakistan.

As they set off for the border, she and her 9-year-old brother painted the old Afghan flag with its red, green and black colors on the back of their hands and sang songs the entire way. She tried to put aside the warnings her friends gave her about the Afghanistan she was heading toward — and the restrictions on women the Taliban had imposed.

Upon passing the border fence, she saw the Taliban’s white flag. A sense of unease fell over her. She pulled the sleeves of her black hijab over the flag on the back of her hand.

“The old flag was beautiful,” she said. Then she whispered, “I can’t say anything negative about the white one now.”

Taliban officials have said they have established a high commission to provide basic services to returning Afghans and plan to set up temporary camps to house them. Still, many returning Afghans say that offers little solace. Among them are some of the roughly 600,000 people who fled in the past two years after the Taliban seized power, including journalists, activists and former policemen, soldiers and officials who worked for the U.S.-backed government.

For Abdul Rahman Hussaini, 56, returning to Afghanistan felt like entering enemy territory. When the Taliban took over, his former employers at a foreign nongovernmental organization advised him to apply for sanctuary in the United States under a program for Afghans who had worked for U.S.-funded organizations. The program required applicants to be outside Afghanistan to apply.

He and 11 relatives who went with him to Pakistan remained after their three-month visas expired, still awaiting word from the program. “We were living in fear every day; it was like we were in a prison,” he said.

Then came the news about the deportation policy. His landlord evicted him, and then, two weeks later, the police knocked on the door of a friend’s home where his family had moved.

Now, back in his homeland, he was overwhelmed with anxiety. He worried that any chance of U.S. sanctuary was gone. He feared retaliation from the Taliban for his prior work. He had no idea how he would provide for his family.

“Every moment,” he said, “my feeling of fear is growing.”

Zia ur-Rehman contributed reporting from Karachi, Pakistan.