NAIROBI, Kenya — With a stroke of his pen, President Uhuru Kenyatta spared the lives of thousands of prisoners on Kenya’s death row on Monday by commuting their sentences to life in prison.

Kenyan law allows capital punishment and convicts are regularly sentenced to death, but the sentence is almost never carried out; the last execution was in 1987. In colonial times, the British authorities executed more than 1,000 Kenyans who were accused of fomenting revolt.

Kenyan news sites beamed images of Mr. Kenyatta leaning over his desk on Monday, surrounded by top officials, as he signed documents that spared the lives of 2,655 men and 92 women. Kenya’s last president, Mwai Kibaki, did something similar in 2009.

Mr. Kenyatta faces re-election next year. Some analysts said the mass reprieve on Monday may have been intended to make the president appear more compassionate as the election draws near. Mr. Kenyatta remains popular among members of his own ethnic group, the Kikuyu, and he enjoys support from other ethnic groups that belong to his political alliance. But opposition leaders say his government has allowed corruption to flourish.