TEHRAN, Iran -- Iran's hard-line former president Mahmoud Ahmadinejad said Tuesday he won't run in next year's presidential election, ending weeks of speculation after the country's supreme leader discouraged his candidacy.

Ahmadinejad could have posed a strong challenge to President Hassan Rouhani, a moderate who many believe will seek a second term after his administration struck the landmark nuclear accord with world powers.

It also could have exposed the still-lingering wounds in Iran from the widespread unrest that followed Ahmadinejad's contested 2009 re-election.

"By the grace of God, I am proud to continue as a small soldier for the revolution," Ahmadinejad said in a letter to Supreme Leader Ayatollah Ali Khamenei.

Ahmadinejad, known for repeatedly questioning the scale of the Holocaust, predicting Israel's demise and expanding Iran's contested nuclear program, had been touring the country, fueling rumours of a political comeback.

He also wrote and publicly released a letter to President Barack Obama in August asking him to "quickly fix" a U.S. Supreme Court ruling that allows families of people killed in attacks linked to Iran to collect damages from the country's frozen assets.

Ahmadinejad previously served two four-year terms from 2005 to 2013. Under Iranian law, he became eligible to run again after four years out of office, but he remains a polarizing figure, even among fellow hard-liners.

Two of his former vice-presidents have been jailed for corruption since he left office. Iran's economy suffered under heavy international sanctions during his administration because of Western suspicions that Tehran was secretly pursuing nuclear weapons. Iran insists its nuclear program is for peaceful purposes.

Internally, many associate Ahmadinejad with the aftermath of Iran's disputed 2009 presidential election. His victory sparked massive protests and a sweeping crackdown in which thousands of people were detained, dozens killed and others tortured.

The memory of the unrest likely sparked comments Monday by Khamenei, who said he recommended an unnamed candidate not seek office as it would bring about a "polarized situation" that would be "harmful for the county."

In his letter Tuesday, Ahmadinejad confirmed that Khamenei was referring to him.

"You advised me it's not expedient to run and I announced my obedience, following my explanations of my plans," Ahmadinejad wrote. "I hereby inform you regarding the supreme leader's considerations, I have no plan for next year's elections."

Ahmadinejad said the two met on Aug. 30. It wasn't immediately clear what transpired in the weeks between, though it appears Khamenei's comments Monday likely pushed Ahmadinejad into making his announcement.

Without Ahmadinejad in the running, it's unclear whether hard-liners will be able to put up a candidate with similar name recognition to challenge a possible Rouhani run. All Iranian presidents since Khamenei assumed the presidency in 1981 have served two four-year terms in Iran's cleric-governed democracy.

Potential presidential candidates are barred from publicly campaigning for the May 17 election until roughly the end of April, making it even harder to find possible challengers.

Soroush Farhadian, a Tehran-based political analyst who backs reformists, said that if Ahmadinejad had won Khamenei's approval, he "could dominate" among other conservatives.

"He built up his election campaigns across the country to run and he wanted to get the supreme leader's approval," Farhadian said. "He believed that if he and his campaign machine was ready, he could force conservatives to pick him as the prime candidate."