A primary school building is to be demolished and rebuilt after it became unsafe due to extensive mould and rotting timber.