Saudi Arabia's crackdown on foreign workers has thrown millions of lives into turmoil and caused rioting in big cities, but the economy should benefit in the long run as Saudi nationals fill the gaps and cut their dependence on the state.
Nearly a million foreigners have left Saudi Arabia since March, when authorities stopped turning a blind eye to visa irregularities they had tolerated for decades, and tens of thousands more have been detained in raids on offices and marketplaces that began this month.
Though most of the roughly 10 million foreigners in the kingdom are expected to remain, alongside a Saudi population of 20 million, the crackdown is part of government efforts to nudge more Saudis into jobs, tackling a problem seen by many as one of the biggest challenges facing the world's top oil exporter.
A majority of working-age Saudis do not have jobs, and most who do are employed by the state, often in what economists call well-paid sinecures that bloat an already flabby bureaucracy.
Official unemployment is just 12 percent, but that excludes a much larger group of people who are not actively seeking work.
Saudi Arabia's ruling family has long used public employment to distribute oil revenues to its people. In an absolute monarchy, it helps the government maintain its legitimacy.
In 2011, when Arab Spring protests were challenging the rule of autocrats across the region, King Abdullah announced hundreds of thousands of new government jobs, pay rises and bonuses, unemployment assistance and cheap housing worth $110 billion.
But as the population grows and higher domestic energy use threatens to eat into oil exports, the state's now-bulging coffers will struggle to maintain such a generous wage bill.
All previous efforts to raise Saudi private-sector employment through market-friendly reforms and more punitive measures such as "Saudisation" hiring quotas have stumbled on the ready availability of cheap foreign labour.
Business owners have also complained that Saudis work less hard than foreigners and won't take jobs they see as menial.
This time, by cracking down on visa irregularities that allowed companies to cheat the system, and by spending billions of dollars on vocational training for young Saudis, the authorities hope their policies will be more effective.
"The likely impact is very hard to quantify because there are so many moving parts," said Steffen Hertog, author of Princes, Brokers and Bureaucrats, a book on Saudi economic policymaking.
But he and other economists interviewed by Reuters said they expected the tough new policies to improve the kingdom's economy in the long run despite some disruption now.
The government is well placed to weather any short-term costs after years of record budget surpluses that have created foreign currency reserves equal to more than 100 percent of gross domestic product. Annual real economic growth has averaged 6.3 percent in the past five years, according to IMF figures.
Small unregistered companies - mechanical workshops, cheap restaurants and grocery shops - face the biggest immediate impact, and the kingdom's newspapers have documented the consequences, including drinking water deliveries cancelled, crops not harvested and school classes suspended.
But much goes unrecorded.
"An element of the labour crackdown will not be captured in the official numbers. Many of these people are not working in the formal economy, so you don't see it in the data," said Paul Gamble, director, sovereign group, Fitch Ratings.
Many small businesses are illegally owned and run by foreigners, while a Saudi is paid to put his name to any official paperwork.
Hertog said closures would have an effect on daily life in the short term, raising prices of some services, but would probably not have much impact on the wider economy.
More immediate problems are faced in the construction sector, which relies on a plentiful supply of cheap workers.
A shortage of workers and a new $640 annual fee companies have to pay to hire expatriates have caused delays to many projects, pushed up construction prices and forced some companies out of business, local press has reported.
Higher construction costs will eventually be passed on to clients, and delays are causing bottlenecks for businesses attempting to expand, but most big projects in Saudi Arabia are commissioned by state-owned companies.
"40 percent of the medium and small companies have been hit now ... new tenders are becoming more expensive," said Fahad al-Hammady, chairman of the contractors' committee at the Saudi Chambers of Commerce.
But despite the exodus of foreign workers in the past 10 months, second-quarter growth in the non-oil private sector was 4.2 percent, official figures show, and Capital Economics expects the non-oil sector to grow 5-6 percent this year.
Meanwhile, the official inflation rate has remained stable, and SABB HSBC's monthly purchasing managers' index showed companies reporting little impact on input prices.
One early benefit of having more workers registered properly should be better economic planning.
"It's an audit of the labour market. Until they know who is there and what they're doing, it is difficult to calibrate policy," said Gamble.
Transparency is particularly important for implementing labour reforms, in which some sectors such as retail have been targeted for higher rates of Saudi employment.
Economists also said private-sector productivity should improve if labour becomes more expensive.
"Companies have to be more efficient and less labour oriented, using technology better. It could lead to an output shift in how the economy operates," said John Sfakianakis, chief investment strategist at Masic, a Saudi investment company.
The biggest economic advantage of replacing foreign workers with Saudis, however, is raising household income, thereby boosting consumer spending. That is already being felt.
Muhammad al-Agil, chairman of Jarir Marketing, the biggest listed Saudi retailer, told Reuters last month that rapidly growing sales over the past two years were partly due to the higher number of Saudis with jobs.
Instead of money being sent overseas by expatriates, he said, it was now being spent in Saudi shops.
In May the Labour Ministry said its new policies had created over 600,000 jobs for Saudis in the previous year. But it is not yet clear if those jobs are sustainable.
In the past some have taken on Saudis in meaningless positions just to meet the quotas. Others have said locals were unqualified or reluctant to take the jobs on offer.
Nobody expects Saudis to work as manual labourers on construction sites or as domestic servants, but in recent years a growing number of young Saudis have taken jobs, such as shop assistants, that they may have disdained in the past.
Agil said two fifths of his shop-floor employees and 85 percent of his office workers are Saudis.
Higher Saudi employment in the private sector would also reduce the burden on the state of efforts to lift living standards with higher government wages and other handouts.
"From Fitch's perspective the whole labour market reform is something we view positively," said Gamble.