PLEASANTON -- Safeway announced Wednesday it will sell its Canada operations for $5.68 billion in a megadeal that provides the supermarket giant with a stockpile of cash.

Shares of Safeway soared 32 percent in after-hours trading.

"The substantial cash proceeds from this transaction will allow us to create value for Safeway stakeholders and contribute to the growth of the ongoing business," said Robert Edwards, Safeway's CEO.

The buyer, Canada-based food retailer Sobeys, will gain 213 full-service grocery stores in western Canada along with 199 in-store pharmacies.

After taxes and expenses, Safeway will net $3.92 billion from the sale, the company said Wednesday.

Safeway will use the proceeds to pay down $2 billion in debt, with most of the rest to be used to buy back Safeway shares. Some cash may be used for growth opportunities.

"They may look for expansions, or acquisitions, or renovate some stores," said Patricia Edwards, a principal analyst with Washington state-based Trutina Financial.

The deal is due to close before year's end, pending regulatory approvals.

In April, Safeway used an initial public offering to spin off Blackhawk Network Holdings, a gift-card provider owned by Safeway. The Blackhawk IPO raised $230 million.

"The whole idea is for Safeway to raise more cash," Edwards said. "That's what they're doing, including the IPO."

Over the 12 months that ended on March 23, Canada Safeway earned $419 million in operating profits. Safeway remains responsible for $294 million in debt for the Canada operations due in March 2014.

The deal comes at a time when Safeway faces more competition in Canada.

Wal-Mart has been pushing into that market, along with Target. Safeway's sales per square foot were about 20 percent below the market average in its western Canada territory, according to David Livingston, a Milwaukee-based retail analyst.

"Safeway will get cash to solve a lot of the problems they have," Livingston said. "When you can get rid of debt, buy back shares, and invest in stores, that is always a good thing. And Safeway is escaping a situation in Canada that was only going to get worse."

Contact George Avalos at 408-373-3556 or 925-977-8477. Follow him at twitter.com/george_avalos.