ANSWER:

This is permissible especially that it was a masjid for centuries. 

Praise be to Allaah.

Yes, it is permissible to buy it and make it into a mosque, but they have to remove all the crosses and images that are hanging or engraved in it, and everything that indicates that it is a church. We know no reason why this should not be allowed.
From Fataawa al-Lajnah al-Daa’imah, 6/267.

Among the evidence that it is permissible to turn the places of worship of shirk and kufr into mosques is the following hadeeth: From Talq ibn ‘Ali (may Allaah be pleased with him): “We went out as a delegation to the Prophet (peace and blessings of Allaah be upon him). We gave bay’ah (oath of allegiance) to him and prayed with him. We told him that in our land we had a place of worship [the word used indicates a Jewish or Christian place of worship], and we asked him to give us some of the water left over from his wudoo’, so he called for water and did wudoo’ and rinsed out his mouth, then he poured that water into a vessel and told us, ‘Go, and when you reach your land, knock down that place of worship and sprinkle this water over the place, then use that place as a mosque.’ We said, ‘Our land is far away and the heat is intense, the water will dry up.’ He said, ‘Add more water to it, it will only increase it in goodness.’ So we set out and when we reached our land, we knocked down our place of worship, sprinkled water on the place and used it as a mosque. We called the adhaan there, and when the monk, a man from Tayyi’, heard it, he said, ‘The call of truth!’ then he climbed one of our hills and we never saw him again.” (Reported by al-Nasaa’i, 694). And Allaah knows best.

The building itself does not need to be destroyed so long as its roof, walls etc. can be put to use and architectural alterations can be made, after removing all the symbols of Shirk and other forbidden things, so that the structure of the building will match the rows of prayer, facing the direction of the Ka’bah. Allaah is the source of strength and He knows best.

https://islamqa.info/en

I wanted to ask is it ok for hagia sophia to be a mosque since it was a church before a place of worship for the disbelievers

Rulings on mosques |