Spaces disappear (but not their application windows) when I unplug external monitors I use Spaces (aka Desktops) to organize my work--I have a Space for email &amp; Omnifocus, a Space for chat &amp; calendar, and a Space for each of my main projects, plus a couple of full-screen application windows which are of course their own Spaces. When I'm at my desk, I have two monitors plus the screen of my MacBook Pro, and I have certain Spaces on each screen. When I unplug my laptop for a meeting, of course all the Spaces move from the external displays to the laptop. That isn't a problem. What is a problem is that application windows have suddenly moved to other Spaces. It looks like the Mac has decided a couple of Spaces don't exist and shoved their windows into other Spaces.

For example, right now I have chat and calendar in Desktop 6, which is currently displayed on my laptop screen while I type this on one of the other monitors. Then I unplug the external monitors, suddenly only four numbered Desktop Spaces exist (plus both the full-screen application windows), and chat and calendar are part of Desktop 1 along with the windows that had already been part of Desktop 1. So basically the problem seems to be that some Spaces are dropped and their apps get shoved into whatever existing Spaces are available. What might be causing that and how can I avoid it?

When I plug the external displays back in, suddenly I have six numbered Spaces again and windows that were originally together in one Space are together again, but the Spaces are numbered differently and some of them have moved from one screen to another. For example, chat and calendar (which were originally Desktop 6 and then got shoved into Desktop 1) are now Desktop 5, and they're on an external monitor instead of the laptop screen. In fact, every Space that used to be on the laptop screen is now on that external monitor, and vice versa.


Why do Spaces disappear when I unplug external monitors, and how do I stop it from happening?
How can I get Spaces to return to the same monitors they were on before, when I plug those monitors back in?


Other details:


MacOS 10.12.6
External monitors are connected via CalDigit Thunderbolt 3 dock
System Preferences -> Mission Control has the following settings checked:


When switching to an application, switch to a Space with open windows for the application
Group windows by application
Displays have separate Spaces
@vykor pointed me in the right direction: the behavior I was seeing is a side effect of the "Displays have separate Spaces" setting.


Why do Spaces disappear when I unplug external monitors, and how do I stop it from happening?


Why: because displays have separate Spaces, which means when there are fewer displays there are fewer Spaces. (One of those things that's obvious in retrospect.) 
How to stop it: unchecking the "Displays have separate Spaces" setting does stop Spaces from disappearing, but the new behavior isn't any better for my needs: windows end up jumbled up on a single monitor when I reconnect the monitors, and all screens cycle through Spaces in unison making it hard to make good use of the screen real estate.

How can I get Spaces to return to the same monitors they were on before, when I plug those monitors back in?


There doesn't seem to be a built-in way to do this, so the answer here seems to be to install a window management app such as Stay.



I've turned "Displays have separate Spaces" back on, and I'm setting up Stay to have a configuration for the laptop display alone and for the three monitor setup, so that I can quickly restore my windows to my desired arrangement in either situation. I'm still working the kinks out with Stay, but I think that's going to be a good solution.