The Justice Department is suing Walmart, alleging that the nation’s largest retailer knowingly filled thousands of problematic prescriptions that helped fuel the opioid crisis.

A lawsuit filed Tuesday in U.S. District Court in Delaware contends that Walmart failed to properly screen prescriptions, and prioritized speed and profit over patient well-being at its 5,000 pharmacies.

“As one of the largest pharmacy chains and wholesale drug distributors in the country, Walmart had the responsibility and the means to help prevent the diversion of prescription opioids,” Jeffrey Bossert Clark, acting assistant attorney general for the civil division, said in a statement. “Instead, for years, it did the opposite — filling thousands of invalid prescriptions at its pharmacies and failing to report suspicious orders of opioids and other drugs placed by those pharmacies.”

The government is seeking civil penalties that could total billions of dollars, according to the news release.

Walmart called the investigation “tainted” and said in a statement that the Justice Department should focus on “bad doctors” who write prescriptions instead of blaming the pharmacists that fill them.

“This lawsuit invents a legal theory that unlawfully forces pharmacists to come between patients and their doctors, and is riddled with factual inaccuracies and cherry-picked documents taken out of context,” the company said.

The lawsuit comes after lawsuits filed last summer by cities, towns, counties and Native American tribes across the country alleging that retailers such as Walmart, CVS, Rite Aid and Walgreens played a role in driving the opioid epidemic by distributing billions of pills. A number of those federal trials, in states including Ohio, West Virginia and Texas, have been delayed during the pandemic.

Last month, Purdue Pharma, the maker of OxyContin, pleaded guilty to three felonies and agreed to an $8.3 billion settlement with the Justice Department for its role in a crisis that has killed more than 400,000 Americans in the past two decades.

Roughly 50,000 Americans died of opioid overdoses last year, a record, federal data shows, and medical experts have warned that the coronavirus pandemic and economic crisis have led to new surges in opioid deaths.

Legal experts said the government’s case against Walmart is unlikely to be resolved quickly, which means the incoming Biden administration will have to decide whether to pursue it.

“The filing today multiplies the legal problems that Walmart faces,” said Carl Tobias, a professor at the University of Richmond School of Law.

In October, Walmart preemptively sued the Justice Department and the Drug Enforcement Administration, asking a federal court to clarify its pharmacies’ roles and responsibilities in filling opioid prescriptions. The company, which is based in Bentonville, Ark., said that its pharmacists had refused to fill hundreds of thousands of opioid prescriptions that they deemed problematic and that it had blocked thousands of “questionable doctors” from having prescriptions filled at its 5,000 pharmacies.

“We are bringing this lawsuit because there is no federal law requiring pharmacists to interfere in the doctor-patient relationship to the degree DOJ is demanding,” the company said in a statement at the time. “Unfortunately, certain DOJ officials have long seemed more focused on chasing headlines than fixing the crisis.”

Walmart ordered 5.5 billion oxycodone or hydrocodone pills from 2006 to 2012, making it the nation’s third-largest buyer of those pills, behind Walgreens and CVS, according to an analysis of DEA data by The Post.

Walmart operates 11,500 stores worldwide, including 5,300 Walmart and Sam’s Club stores in the United States. It is the nation’s largest private employer, with roughly 1.5 million workers.

The Wall Street Journal was first to report on the DOJ lawsuit. Walmart shares fell 1.2 percent, to $144.20, in Tuesday’s trading. The company’s stock has climbed 21 percent so far this year.