A man who was mocked mercilessly online for brazenly shaving aboard an NJ Transit train was just trying to clean up after spending days in a homeless shelter, he said.

“My life is all screwed up. That’s the reason I was shaving on the train,” Anthony Torres, 56, told the Associated Press.

A video of Torres sitting in his seat and swiftly swiping at his lathered face with a razor on the train out of Manhattan on Thursday night was posted on Twitter. By Monday, the since-deleted clip had 2.4 million views, and a second video was posted showing a clean-shaven Torres with a beer in hand.

Twitter users hurled insults at Torres in response to his public grooming, including “slob,” “animal” and “nasty.” Others applauded his steady hand or joked that someone should buy him an electric razor.

“I am SHOCKED he didn’t cut his entire face open like that. It takes skill,” wrote @JayShams.

The truth is that Torres had been staying in a homeless shelter in New York and reached out to his family in New Jersey for help. A brother gave him money for the ticket leaving Penn Station at 7 p.m. Thursday toward Trenton — to stay with another sibling.

Torres left the shelter before getting the chance to shower and clean up, he said, adding that he wanted to look “presentable.”

“I don’t want to say that I’m homeless, let everybody know,” he said. “That’s why I was shaving.”

Now Torres is staying with his brother Thomas, 57, in Atco, NJ.

They grew up poor with four other brothers, raised on a farm in Hammonton, NJ, about 35 miles outside Philadelphia, Thomas Torres said.

Anthony worked a number of odd jobs, including casino security guard and then construction — moving to wherever his gig was and spending time living in motels or sleeping in bus depots.

When he arrived at his brother’s house, he asked for a sleeping bag, saying he was prepared to go spend the night under a bridge.

“For so many years, he’s lived this way and I feel sorry for him. It’s hard to see the life that he’s lived,” Thomas Torres said, his voice filled with tears.

Anthony said he didn’t realize he was being filmed and he was amazed — and upset when he realized a video of him was being shared online.

“I never thought it would go viral, people making fun of me,” he said.

Thomas added: “Maybe people will have more feeling knowing what [I’ve] been through.”