I think they keep the S in production because Elon wants it. Since the Model 3 has become available in large numbers, the S and X have suffered. I like the S more than the Model 3, but I would have bought a 3 when I bought if it was available. They have been ratcheting down the price of the S and X since the 3 was introduced. The S now costs 25% less than it did 4 years ago with more range and a more capable self driving system as well as some other upgrades.
If the S/X get down to less than a $10K premium over the 3/Y they can probably sell a fair number of them, but the 3/Y will continue to sell better. The S and X are bigger than most cars sold around the world. the 3/Y are large for many countries and are the most common size for American cars. The only sedans that have been selling at all the last few years are the Accord, Camry, Corolla, Malibu, Fusion, etc. In the US consumers prefer SUVs and in most of the rest of the world consumers prefer hatchbacks.
The 3/Y are the right size and right price for the market. The S and X have always been a bit too large for most people. My SO likes smaller cars. She loves the tech and power in the S, but does not like the size at all. She has said if money was no object she'd be getting a SpaceX Roadster. She thinks the 3 and Y are too big for her. Her tastes run smaller than the norm, but most Americans either prefer something the size of the 3/Y or they go for a big pickup. The S and X are in a size niche that consumers have left behind.
The starting price of the S is now $75K and about the only features to add are some trim and full self driving. But as I said above, the S and X are too large for cars outside North America and are on the too large side for North American consumers today. And when a more expensive car is available, there are always more sales of the cheaper cars. BMW makes the 3, 5, and 7 series, and the 3 Series is the best seller by a wide margin.
Tesla is basically making the Model S into the 4 door sports car of the line up. When the Plaid version comes out they will probably sell a fair number of those the first year to people replacing their P100Ds, but the non-Performance version will probably be kept in the line up because it won't cost much to make the cheaper car.