In August 2021, more than three decades after sneaking across the southern border as young adults to work and support their families in Mexico, Irma and Javier Hernandez checked in at La Guardia Airport for a one-way flight from New York to Oaxaca. They were leaving behind four American children, stable jobs where they were valued employees and a country they had grown to love.

But after years of living in the United States without legal status, the couple had decided it was time to return to their homeland. Ms. Hernandez’s mother was 91, and they feared she might die — as Ms. Hernandez’s father and in-laws did — before they saw each other again. With dollar savings, they had built a little house, where they could live, and had invested in a tortilleria, which they could run. Their children, now young adults, could fend for themselves.

“Only God knows how hard we worked day after day in New York,” said Ms. Hernandez, 57. “We are still young enough that we could have kept going there, but ultimately we made the difficult choice to return.”

The Hernandezes are part of a wave of immigrants who have been leaving the United States and returning to their countries of origin in recent years, often after spending most of their lives toiling as undocumented workers. Some of them never intended to remain in the United States but said that the cost and danger of crossing the border kept them here once they had arrived — and they built lives. Now, middle-aged and still able-bodied, many are making a reverse migration.

Mexicans, who represent the largest and most transformative migration to the United States in modern history, started a gradual return more than a decade ago, with improvements in the Mexican economy and shrinking job opportunities in the United States during the last recession.

But departures have recently accelerated, beginning with crackdowns on immigrants under the Trump administration and continuing under President Biden as many older people decide they have realized their original goals for immigrating and can afford to trade the often-grueling work available to undocumented workers for a slower pace in their home country.

Their departures are one of many factors that have helped keep the total number of undocumented immigrants in the country relatively stable, despite a flood of migrant apprehensions at the southern border that reached two million last year.

“It’s a myth that everyone comes here and nobody ever leaves,” said Robert Warren, a senior visiting fellow at the Center for Migration Studies, a think tank, who wrote a recent report on the trend.

“There’s a lot of people leaving the country, and they’re leaving voluntarily,” said Mr. Warren, who is one of several demographers, including academics at Emory University, Princeton University and the University of California, Los Angeles, who have been documenting the trend.

The current undocumented population has stayed relatively constant at about 10.2 million over the past several years after peaking at nearly 12 million in 2008, even with the large number of new arrivals at the border.

An emergency health order adopted to slow the transmission of the coronavirus has allowed border authorities to quickly expel more than 2.5 million of the new arrivals since 2020; hundreds of thousands of others have been allowed to enter the country during that period. But a largely voluntary exodus of other immigrants has kept overall population numbers relatively steady, demographers say. (While deportations accelerated under both the Obama and Trump administrations, those numbers were too small to be a significant factor.)

The number of undocumented people from about a dozen countries, including Poland, the Philippines, Peru, South Korea and Uruguay, declined 30 percent or more from 2010 to 2020.

The undocumented population from Mexico, the principal source of immigrants to the United States, dropped to 4.4 million from 6.6 million during that period.

Declines were recorded in all but two states during the decade, plunging 49 percent in New York; 40 percent in California, which lost 815,000 Mexicans; 36 percent in Illinois; and 20 percent, or 267,000, in Texas. The data suggests that those residents were not moving to other states but returning to their home countries, Mr. Warren said.

There has long been an ebb and flow in undocumented immigration. People leave home in response to push factors, such as financial duress, drought and escalating violence, as well as in response to pull factors in the United States, chiefly jobs and safe haven.

The number of undocumented Polish immigrants shrank by half from 2010 to 2019 amid improving conditions in Poland. Brazilians returned in large numbers when their country’s economy was thriving, thanks to a food export boom and successful bids to host the 2014 World Cup and 2016 Olympics that spurred a construction bonanza.

Rubén Hernández-León, a sociologist at U.C.L.A. who has conducted field research of Mexicans who have returned home, said that the primary reason people gave for leaving the United States was a desire to reunite with family.

The anti-immigrant rhetoric of former President Donald J. Trump coupled with his administration’s crackdown on unlawful immigration caused anxiety that also drove some undocumented people, especially Mexicans, to leave, Mr. Hernández-León said.

A return to the homeland has always characterized Mexico-U.S. migration. For a long time, mainly men alone traveled back and forth between their villages and the United States, earning dollars during monthslong stays.

This circular migration was upended in the early 1990s as the United States introduced a spate of policies to fortify the border, erecting barriers and deploying more agents.

But the border restrictions backfired. After facing risks and paying smugglers to cross the border, undocumented workers stayed in the United States, rather than coming and going.

“Most of them never wanted to stay. We gummed up the works when we militarized the border,” said Douglas S. Massey, an immigration scholar at Princeton. “They spent longer and longer time and had families.”

Now, he said, census data suggests that many of them are electing to go home.

“If they have savings and a house in Mexico, they can retire there,” he said. ”Their kids born in the States are now old enough to take care of themselves and can go back and forth to visit.”

Ms. Hernandez left her Mexican pueblo in 1987 “por la necesidad,” she said.

In New York, she settled into nanny jobs with families in Manhattan and sent money home. She fell in love with Javier, a fellow Oaxacan who had immigrated around the same time and was learning the art of making pizza. They married and their first child, Jennifer, was born in 1992.

Without legal status and with the border increasingly barricaded, the Hernandezes could not risk leaving the United States.

Mr. Hernandez’s parents passed away, and he mourned them from afar, unable to attend their funerals. Ms. Hernandez’s father died.

“Years passed, and we harbored hopes that we could secure papers to move freely between both countries,” Ms. Hernandez said.

The last amnesty program passed by Congress in 1986 enabled 2.3 million Mexicans to legalize their status. Since then, Democrats and Republicans have failed to reach a consensus on another immigration reform bill again and again.

For Mr. and Ms. Hernandez, years in the United States turned into decades. Along the way, the couple had a son and then a set of twin boys.

Jennifer eventually attended divinity school at Harvard University, and then returned to New York to work for Make the Road New York, an immigrant advocacy group; her three younger siblings finished high school. When she turned 21, Jennifer could sponsor her parents for green cards, but they quickly realized it was a process that would take more than a decade to complete.

“We started having this real serious conversation about them going back so that my mother could have time with her mother before she passed away,” Jennifer said.

The couple figured they could make ends meet selling the maize, cabbage and herbs grown on their small plot of land in Mexico and the tortillas from the factory they had recently acquired. Their children, now adults, assured them that they could help support them, if necessary. But it was a heart-wrenching decision to make — even though the couple had always thought of going home.

The children Ms. Hernandez had helped raise as a nanny in New York were distraught.

“It took a year until we pulled the trigger,” Jennifer said.

All four Hernandez children joined their parents on the plane to Oaxaca, and after settling into the house there they all took their first-ever vacation as a family, a week on a Mexican beach. Then the children boarded a flight back to the United States.

“We cried all the way to New York,” Jennifer recalled. “It’s been a year and a half, and it’s still very hard,” she said, her voice cracking.

Ms. Hernandez said she still hoped to return to New York one day, at least for extended visits, if Jennifer is eventually able to secure green cards for the couple.

“I have my children there, and one day they will have children,” she said. “I will want to care for my grandchildren.”