SAN FRANCISCO — Officials in six San Francisco Bay Area counties issued a sweeping shelter-in-place mandate Monday affecting nearly 7 million people, ordering residents to avoid any unnecessary travel by any method and only leave their homes for food, medicine and exercise.

The order says residents must stay inside and venture out only for necessities for three weeks starting Tuesday in a desperate attempt by officials to curb the spread of the novel coronavirus.

“I know today’s order is a radical step. It has to be. We need to act now, all of us, to protect the public health,” said Dr. Grant Colfax, director of the San Francisco Department of Public Health.

If affects the counties of San Francisco, Marin, Santa Clara, San Mateo, Alameda and Contra Costa, as well as the city of Berkeley.

“We must move aggressively and immediately,” said San Jose Mayor Sam Liccardo, whose city is the epicenter of Bay Area outbreak. “History will not forgive us for waiting an hour more.”

San Francisco Mayor London Breed criticized what she called a “terrible response” by federal officials.

“We can’t sit back and wait for the federal government to do anything. Unfortunately, they have not been as proactive as they should in recognizing this is a crisis throughout the world,” she said.

The order tells people to work from home unless they provide essential services such as public safety, sanitation and medical services. Pharmacies and banks will remain open. Restaurants will be open only for take-out, Breed said.

Kevin Jones, general manager of Buena Vista Cafe, an iconic San Francisco restaurant that has been a draw for tourists since 1952 in the popular Fisherman’s Wharf, said the order is “going to hurt, but our duty is to protect our employees and our customers.”

The cafe was the only one open Monday in one of the busiest tourist areas of the city and almost full. He said he’s worried about the 58 employees being able to pay their rent; owners and managers decided that any perishable foods would go to the workers.

The dramatic step came as officials across California took increasingly strident steps to separate people and contain the spread of coronavirus, prompting millions of the state’s oldest and youngest residents to stay home Monday.

Freeways normally jammed with rush hour drivers flowed freely. Long lines persisted at some grocery stores despite despite officials saying there’s no need to hoard supplies.

Responding to panic buying, executives of major grocery chains said at Los Angeles press conference that supply chains are in good shape and clerks are working to restock shelves and clean.

“There is plenty of product,” said Bryan Kaltenbach, president of Kroger’s Food 4 Less.

Gov. Gavin Newsom on Sunday urged the state’s 5.3 million people who are 65 and older and those with chronic health conditions to isolate at home. He also called for all bars, wineries, nightclubs and brewpubs to close.

Los Angeles Mayor Eric Garcetti ordered restaurants to only offer pickup, delivery and drive-through service. He also closed gyms, movie theaters, bowling alleys and arcades.

Newsom said an estimated 85% of California’s 6 million public school students will be out of their classrooms this week. Many districts are arranging for meal pickups for families that depend on schools for breakfast and lunch. Movie theaters, casinos and theme parks were told to limit crowd sizes and keep patrons separated.

The rapid pace of new restrictions caught people and institutions off guard.

Near San Diego State University, Will Remsbottom’s Scrimshaw Coffee had just three people and one was washing windows. “I’m just struggling with this moral conundrum of remaining open and being a potential spreader versus closing and not being able to pay my employees,” he said.

Robert Murillo’s younger daughter turned 4 on Sunday, but the family stayed home and ate cupcakes. “They’re still at the age that blowing a balloon makes their day, so thank God for that,” he said.

California has confirmed at least 335 cases of the virus and six deaths. The virus usually causes only mild or moderate symptoms, such as fever and cough, but can be deadly for older people and those with underlying health conditions.

Meanwhile, California is working with Verily, the life sciences arm of Google parent company Alphabet, on a tool to help people determine whether they need to be tested and where to find testing.

The state has increased the number of available hospital beds and will reopen shuttered medical centers in the coming days. Newsom said he was pushing to move many of the state’s 108,000 homeless people indoors, using motels and 450 state-owned trailers set up before the outbreak.