We have heretofore discussed that al-Muizz had vested Buluggin bin Ziri (d. 373/984) with the governorship of all the Fatimid dominions in the Maghrib except for the Kalbid Sicily and Tripoli in 361/972. Later on, Buluggin asked Imam al-Aziz to give him rule over Tripoli as well. His request was granted and from 365/975, Tripoli began to be ruled by the Zirids. Buluggin appointed Tamsulat bin Bakkar as the amir of Tripoli, who governed the province for 20 years. In 386/996, after the death of Mansur, the second Zirid ruler, the relation between Tamsulat and Badis (d. 406/1016), the third Zirid ruler were strained. Tamsulat wrote to Cairo, asking Barjawan to send a new amir for Tripoli. Barjawan's error was that without the consent of Badis, he appointed Yanis as the amir of Tripoli in 388/998, who was then the amir of Barqa. Badis wrote a letter to Yanis, asking for an explanation of his move from Barqa to Tripoli, but he received no satisfactory reply. Realizing the danger that Yanis represented, Badis sent his troops into battle against him. In the ensuing battle, Yanis was killed and his forces retreated to Tripoli, where they barricaded themselves awaiting help from Cairo.
The above military actions of Barjawan in Tripoli supported no decree from al-Hakim. It however affected the relations between the Fatimids and the Zirids. In addition, Tripoli, over which the dispute had begun, was occupied neither by the Fatimids nor by the Zirids, but it came in the hands of the enemy of both, i.e., the Banu Zanata. Fulful (d. 402/1011), the chief of Zanata tribe had taken an opportunity and proceeded towards Tripoli. He entered the city and declared his support against the Zirids and proclaimed his loyalty to the Umayyads of Spain.
Hence, the Fatimids lost Tripoli for about ten years (390-400/999-1009). After restoration of peace in Egypt, al-Hakim turned his attention towards Tripoli. He dispatched his forces at the command of Yahya al-Andulusi as a new amir of Tripoli, and commanded Raydan at Tripoli to give Yahya a sum of money for expenses. Raydan, who most probably appropriated the money, instead gave Yahya a signed order to collect money from Barqa. When Yahya reached Barqa, he found the state treasury depleted. Most of the soldiers in his troops belonged to Banu Qorra, whom he had promised generous payment. Thus, Yahya faced difficulties in the field. Banu Qorra not only deserted Yahya, but they also raided his camps in angry and pillaged whatever they found and returned to their territory. Henceforward, Yahya entered Tripoli with the remaining troops. He was overpowered by the Zanata chief, Fulful, who humiliated him and took control of Tripoli, proclaming his loyalty to the Umayyads of Spain. On other side, al-Hakim did not send any reinforcement to regain Tripoli, and as a result, the Fatimids lost their suzerainty in Maghrib. Their relations with the Zirids also deteriorated, and the Sanhaja tribe ruled there independently. Later on, the Fatimid khutba was also removed.