John Kelly said Thursday that he hated leaving his gig as head of Homeland Security to become President Trump’s chief of staff — but that “God punished me.”

“The last thing I wanted to do was walk away from one of the great honors of my life, being the secretary of Homeland Security, but I did something wrong and God punished me, I guess,” Kelly, in an apparent joke, said at a celebration of the department’s 15th anniversary, The Hill reported.

Trump named Kelly, a retired Marine general, his chief of staff in July 2017, and Kelly’s main responsibility was to bring order to a chaotic White House, which was riven by infighting among competing factions in the West Wing.

“I wish I had worked harder in the six months I had in the job to not only better protect the men and women that get beat up so badly every day, but to really advertise in a much more effective way how good you are,” Kelly added in his remarks.

His job security has become a question mark inside the Beltway, with various reports saying that Trump has been furious at times with his chief of staff, who in recent days downgraded the security clearance of Jared Kushner, the president’s son-in-law and senior adviser.

Kushner, 37, had been privy to highly classified materials, including the President’s Daily Briefing, and had a broad portfolio in the administration — from brokering a Middle East peace deal to reorganizing US law enforcement to solving the opioid crisis — that will necessarily be scaled back now that his clearance has been downgraded.

Kelly also came under the magnifying glass for his botched handling of the case of Rob Porter, the administration’s former staff secretary, who was eventually forced out after revelations that his two ex-wives had accused him of domestic violence.

The chief of staff first praised Porter, but then insisted he acted swiftly to can him immediately after learning of the allegations.

But FBI chief Christopher Wray told lawmakers under oath that the FBI’s background check into Porter had been completed months earlier, and included the allegations of wife beating.

Kelly then insisted that the matter was “all done right.”

But the president has also praised his chief of staff on Twitter.

“Thank you to General John Kelly, who is doing a fantastic job, and all of the Staff and others in the White House, for a job well done. Long hours and Fake reporting makes your job more difficult, but it is always great to WIN, and few have won more than us!” Trump wrote.