My reading of the article suggests limitations to your interpretation. (These may be from being an American reading about Sweden through a British news source.)
Sweden's drug policies seem rather prohibitionist. https://en.wikipedia.org/wiki/Drug_policy_of_Sweden says there's "zero tolerance focusing on prevention, treatment, and control, aiming to reduce both the supply of and demand for illegal drugs", and that cannabis is still a Schedule I drug.
It therefore seems another answer is that Swedish drug prohibition policies have failed. There's clearly a demand ("Large quantities [of narcotics] were detected regardless of how wealthy or poor the district"), and the article says funding for the gangs comes from selling drugs.
If immigrants were magically removed, there will still be the demand for illegal drugs, yes? I find it hard to believe no one will fill that gap.
Also from the article, why are immigrants geographically segregated and socially excluded?
I ask because the article is also clear that the older policing policy, where officers didn't know the community, caused part of the failure. ("The turnaround had been built on old-school policing, he said - talking to everyone, including the Ali Khans ... winning back the trust of Hjällbo's residents offers a back-to-basics reminder for UK policing".) Why did 'old-school policing' disappear in immigrant neighborhoods?
And isn't a third answer that many other EU countries (and my country, the US) didn't accept refugees anywhere near the extent that Sweden and Germany did, seemingly out of xenophobic fears?
Eg, 1 million Irish economic refugees immigrated to the US after the Great Famine. The US accepted 22K Syrian refugees [1] from 2010-2020, and 24 governors didn't want those refugees settled in their state [2]. Sweden accepted 114K Syrian refugees [3].
[1] https://www.migrationpolicy.org/article/refugees-and-asylees...
[2] https://www.bbc.com/news/world-us-canada-34848248
[3] http://www.statistikdatabasen.scb.se/pxweb/en/ssd/START__BE_... selecting years 2010-2020.