Any terrain provider you add in CesiumJS is considered global. So all you’d need to do is keep track of whether or not you’ve added terrain to your scene, right?
If this isn’t the case, can you reproduce your setup in a Sandcastle? See How to share custom Sandcastle examples