If the waqf is no longer of benefit and it is not possible to benefit from it, it is permissible to sell it according to the correct scholarly opinion, whether it is a mosque or anything else. If the people of the mosque are moving to another place and there will no longer be any one who could benefit from it, it is permissible to sell it and use the money to build another mosque.
Ibn Qudaamah may Allah have mercy on him) said: If a waqf falls into disrepair and is no longer of any benefit, such as if a house collapses and it is not possible to rebuild it, or the people of the village move away from a mosque so people no longer pray in the mosque, or it has become too small for the congregation and it is not possible to expand it where it is, or if its walls have developed cracks and it is not possible to repair them or part of them except by selling part of it, then it is permissible to sell part of it in order to repair the rest of it.
If it is not possible to benefit from any part of it, it is permissible to sell the whole of it.
Imam Ahmad said: If there are two wooden beams in the mosque that are of value, it is permissible to sell them and spend the money on the mosque.
He also said: A mosque may be moved if there is the fear of thieves, if its location is dirty? Al-Qaadi said: i.e., if that is keeping people from praying in it.
End quote from al-Mughni, 5/368.
Shaykh Ibn Baaz (may Allah have mercy on him) said:
If the waqf is no longer of benefit, whether it is a mosque or otherwise, it is permissible to sell it according to the more correct of the two scholarly opinions, and its price may be spent on another waqf of equal value to replace the first waqf if possible. It was narrated from Ameer al-Mu’mineen ‘Umar ibn al-Khattaab (may Allah be pleased with him) that he issued instructions that the mosque of Kufah be moved to another location because there was an interest that could be served by doing so. Therefore when a mosque is no longer of any benefit at all, it is more appropriate to suggest that it is permissible to move it. However, this is a matter concerning which there is a difference of opinion among the scholars, but the reliable view is that it is permissible because Islamic sharee‘ah is perfect and came to achieve and fulfil what is in people’s best interests and to cancel and reduce that which is detrimental to their interests; it enjoins preserving wealth and forbids neglecting or wasting it. There is no doubt that if a waqf is no longer effective there is no interest to be served by keeping it; rather keeping it is a waste of money. So it should be sold and the money spent on something similar.
End quote from Fataawa ash-Shaykh Ibn Baaz, 20/11
If this mosque can be sold to someone who will use it for something permissible, such as a hospital for example, after removing its minaret and changing its appearance, it is permissible to sell it as it is, without demolishing it, because of the interest that will be served which is to bring a higher price than if (the land) were to be sold after demolishing it.
If there is the fear that it will be used for some haraam purpose, then it is to be demolished and the land sold, and the money should be spent on another mosque.
And Allah knows best.