(KHN) Bill Mather, a pharmacist in the small Iowa city of Greenfield, wanted to make sure his neighbors could fill their prescriptions without driving long distances or enduring long wait times.

So when pharmacy chains and big-box stores began expanding into rural markets, he sold his drugstore in 2007 to Pamida, a grocery chain owned by the Shopko department store company, hoping that would keep his practice alive. Then, in 2019, when Shopko declared bankruptcy, shuttering more than 360 stores, he and another Shopko pharmacist helped open a new drugstore for the city of about 2,000 people.

Across the country, pharmacists like Mather are filling the voids left when large drugstore chains and big-box stores with pharmacies pull out of small communities. In some areas, pharmacists who were let go when big chains closed are now opening new drugstores, often in the same locations. In other areas, pharmacy owners from neighboring towns are opening new branches. Without them, numerous communities would have been left with no pharmacy.

It didn’t used to be this way. Big-box stores like Shopko started to move aggressively into the pharmacy market 30 years ago, hoping customers would fill their carts while the store filled their prescriptions. Large chains established beachheads in small towns by acquiring independent pharmacies or driving them out of business.

According to the Bureau of Labor Statistics, the number of pharmacists employed at big-box stores peaked at more than 31,800 in 2012. But as online sales and mail delivery of consumer goods and prescription drugs grew, the big-box stores had no reason to stay. By 2019, the number of big-box pharmacists had dropped to fewer than 18,000. (The bureau no longer reports the number.)

“The big-box stores came into smaller and smaller communities, and they, in essence, outcompeted all the other pharmacies in the area,” said David Zgarrick, a pharmacy professor at Northeastern University in Boston. “Now they’re completely gone and with them the pharmacy services and everything else they provided. They left a vacuum in a lot of these places.”

With CVS Health’s recent announcement that it will close 300 stores a year over the next three years, more towns could lose another tier of pharmacies: the big drugstore chains. CVS has not yet said which stores will close.

“We’re considering a number of factors when making these decisions, including local market dynamics, population shifts, store density, and the needs of underserved communities,” said Mike DeAngelis, a CVS Health spokesperson. “In fact, reaching underserved populations has been a priority for us all through the pandemic, such as the access to testing and vaccines we’ve provided.”

Kathleen Bashur, a spokesperson for the National Association of Chain Drug Stores, said chain pharmacies of all types and sizes play a significant role in meeting the health and wellness needs of communities throughout the nation. “The decision to close a store is a difficult one,” she said.

Zgarrick attributes much of the decline in big-box pharmacies to Amazon and other online merchants that undercut the profitability of their non-pharmacy sales. In the past, big-box stores could treat pharmacies as loss leaders and make up that revenue with sales of other goods. Now, Zgarrick said, big-box stores have to ask tough questions about how to allocate their space: “‘At the end of the day, how are we going to make the most money per square foot? Is it by having a pharmacy or by selling tires?’”

Nelson Lichtenstein, a University of California-Santa Barbara history professor who has chronicled the rise of Walmart, said big-box stores are constantly reevaluating their store locations, closing less profitable stores and opening new ones in places where they think they can make money.

“They just keep a kind of slow churn,” he said.

The cost of building a big-box store is about $10 million, Lichtenstein said, and such stores can net $200 million in annual revenue. Pulling out of a location is therefore not a huge loss for a big company if revenues falter.

“So they will shut down the stores and leave a devastated town because the Walmart put the other guys out of business,” he said.

Walmart did not answer questions about its closures.