A man who was sentenced to 15 years in prison for possessing cocaine has been cleared of all charges after a lab test confirmed that the substance was actually powdered milk.

Cody Gregg, 29, was arrested Aug. 12 after a police officer attempted to flag him down for not having rear lights on his bicycle, according to an affidavit obtained by the Oklahoman.

After a brief chase, the officer who caught him searched his backpack and found a coffee can with a clear baggie that contained “a large amount of white powder substance,” the affidavit continued.

The substance tested positive for cocaine and Gregg was charged with drug trafficking, a felony, and jailed on a $50,000 bond, the Oklahoma City Police Department confirmed.

Court records show that Gregg pleaded not guilty to the drug trafficking charges the next week during his court appearance. However, after about two months in jail, Gregg pleaded guilty in October to the lesser charge of cocaine possession with the intent to distribute and sentenced to 15 years in prison.

Finally on Instagram:Jennifer Aniston shares selfie with all 5 'Friends' co-stars in platform debut

Debunking 15 cruise myths:Yes, you can go on a cruise alone. No, not everyone is drunk

Then a few days later, Gregg returned to court to withdraw his plea after a lab test concluded that the suspicious powder substance was actually powdered milk.

Gregg told the judge that he found the substance in a food pantry and had entered the guilty plea to get out of the Oklahoma County Jail, according to the Oklahoman.

The jail is known for its poor conditions, from mold to overcrowding, and unusually high suicide rates, the Washington Post reported.

Court records show that Gregg has been arrested before for possession of marijuana, meth and drug paraphernalia. In one instance, his detention costs were waived due to mental illness, according to the Post.

Many experts have criticized the use of field testing, also known as presumptive testing, to assist officers identify substances on the field.

According to a May 2018 report by the National Institute of Justice's Forensic Technology Center of Excellence, presumptive testing is used to establish the possibility that a substance may be present and, therefore, can result in false positives and false negatives.

The Oklahoma County Jail said Gregg was released on Oct. 11.

Follow Adrianna Rodriguez on Twitter: @AdriannaUSAT