A California man jailed for six months for eating a leftover cookie without permission while undergoing a residential treatment program has been released.

Gregory Fields, 42, of San Francisco, was caught eating the leftover cookie during an event where his residential treatment program - The Salvation Army Harbor Light Center - was making and handing out lunch to the homeless, the San Francisco Public Defender's office said.

But Fields was walked free after the Public Defender's Office asked the San Francisco Superior Court to release him.

After eating the cookie without asking, the public defender's office said Fields was asked by the program to leave.

Fields worked with his caseworker to have Harbor Light let him stay, and they agreed, if he would start the program again.

According to the Public Defender's Office, starting it over would mean a 30-day detox and then a 30-day blackout period from the outside world.

The office claims Fields has been clean for three months.

In the video below: The most popular Christmas cookies in the US

Your cookie settings are preventing this third party content from displaying. If you’d like to view this content, please adjust your Cookie Settings . To find out more about how we use cookies, please see our Cookie Guide.

Fields, who lives with his mother, declined to go back to the program during a Drug Court hearing last week and was remanded to the county jail by Judge Michael Begert on a six-month jail sentence.

He has requested a new hearing, which takes place Monday morning.

"The program's response was grossly disproportionate to the unauthorised snacking offense, and the court's response to the low-level rule violation is counterproductive and inhumane," Field's attorney, San Francisco Deputy Public Defender Dana Drusinksy, said in a statement last week.

More on 7NEWS.com.au

Fields was placed in the program while serving probation on a vandalism charge from 2017, the public defender's office said.

After he was told to leave Harbor Light, Fields tried to visit the Drug Court for group meetings run by the city's Department of Public Health, but was turned away.

He eventually found another outpatient treatment program, but wasn't offered any other alternatives by Begert or his case workers when he was taken back into custody on December 4, the Public Defender's Office said.

The Salvation Army didn't immediately respond to a request for comment.