Yahya IV was the great-grandson of the second
Idrisid emir,
Idris II, () by a junior line of the Idrisid dynasty. His uncle
Ali ibn Umar had already briefly ruled from the Idrisid capital of
Fes in the late 860s, before being driven off by a
Kharijite rebellion. Yahya IV made
Meknes, to the southwest of Fes, his base. In traditional accounts, Ali is held to have been succeeded by his cousin
Yahya III ibn al-Qasim, who in turn was killed in battle in 905 against Yahya IV's forces. In reality, Yahya III likely did not long rule in Fes and southern Morocco, where members of a third branch of the dynasty, the descendants of Isa ibn Idris II, held sway until . Yahya IV's family apparently supplanted them, and from 905, according to much later sources, Yahya IV ruled in Fes. The northern parts of Morocco remained under the rule of Yahya III's descendants. It was during Yahya IV's reign that the
Fatimid Caliphate appeared in Morocco, soon after its establishment in 909. The
Berber Fatimid general
Masala ibn Habus invaded Morocco, defeated Yahya IV in 919 (or 917), and forced the latter to accept Fatimid suzerainty. Three years later (922 or 919/20, accordingly), Masala returned and deposed Yahya IV outright, taking him prisoner and installing a Berber governor under the overall rule of Masala's cousin,
Musa ibn Abi'l-Afiya. Musa had Yahya tortured to make him reveal the location of is treasures, before sending him to exile in
Asilah. There he remained until 943, when he was sent to the Fatimid capital,
al-Mahdiya. Yahya died there three years later. Fes remained in Fatimid hands until 922 or 928 (the medieval sources differ), when
al-Hasan al-Hajjam, a nephew of Yahya III , led a revolt and briefly drove the Fatimids out. ==References==