Praise be to Allah, the Lord of the Worlds; and blessings and peace be upon our Prophet Muhammad and upon all his Family and Companions.
When a person gives something as an endowment for a specified purpose, that becomes valid and consequently he has no right either to change the purpose for which the object was devoted or to act with it freely.
This is the opinion of the Shafies school and others. So, it is prohibited to build a Mosque on the earth dedicated for an Islamic school. The opinion of the Shafie School is more strict than others concerning this cause. Some of the Shafi scholars do not permit selling the devoted objects even though their benefits no longer exist, whereas the other Shafie scholars permit doing so.
Finally, the preponderant opinion of Shafites and others is that changing the purpose of the Waqf (endowment) is not permitted as long as purpose of the endowed object can be met.
Imam Ibn Qudanah wrote in his famous book al-Mughni: "It is forbidden to change the endowed object as long as it could be kept intact as it is also forbidden to sell the Waqf as long as it could be useful for the purpose for which it was first put".
Allah knows best.