The doctor behind one of the biggest pill mills in New York state history bawled Wednesday as he admitted to illegally peddling millions of oxycodone pills out of his Queens pain management clinic.

Dante Cubangbang bawled openly as he told Manhattan federal Magistrate Judge Ona Wang that he “without legitimate medical purpose wrote prescriptions for oxycodone” from 2012 to 2016.

The 51-year-old stared into his lap as he attempted to choke out the statement, crying so hard at times that his words were unintelligible.

“Take as much time as you need,” Wang said as Cubangbang rubbed his eyes with a tissue.

Prosecutors say Cubangbang’s clinic illegally churned out some 6 million oxycodone pills, raking in around $5.7 million in patient fees alone during the course of the scheme. Court papers say Cubangbang wrote medically unnecessary prescriptions to purported patients, who paid $300 per visit, before filling the prescriptions and selling them to drug dealers.

Cubangbang pleaded guilty to charges of conspiracy to distribute oxycodone, conspiracy to commit health care fraud, and conspiracy to commit money laundering during the brief hearing — though the plea must still be accepted by Manhattan federal Judge Paul Gardephe before it’s official.

Following the proceeding, the disgraced doctor removed his tie, jacket, shoelaces and valuables, and walked sobbing into the pens as he voluntarily surrendered himself into federal custody.

Wang then sealed the transcript of the proceeding.

Two of Cubangbang’s assistants, nurse practitioner John Gargon and receptionist Loren Piquant, are scheduled to stand trial before Gardephe Oct. 15.

Cubangbang’s defense attorney Christopher Cardillo declined to comment when asked if his client was cooperating with prosecutors.