A question of legality might come from German authorities determining if this is solely to avoid tax, which is open-ended. It might be hard for them to make this argument if you can prove you transferred operations to country X to maximize company's growth, access local talent, closer proximity to customers etc.
Regardless, anther commenter pointed out that the exit tax applies to all companies that you own regardless of location. In that case, the approach isn't feasible.
Also it goes without saying, seek your own legal advice rather than trusting random comments on the internet.