These layoffs are about short term costs to meet Wall Street expectations. Every one of these companies are making billions a quarter. And they could easily afford to keep these workers. But that’s just not how the game is played.
The more contextual answer is it's a combination of factors:
Technology companies performed very well during COVID due to the shift to online working and learning. Many companies expanded their workforce significantly during this time, under the assumption that the explosion in need for technology that happened during COVID would continue afterwards due to permanent shifts in working trends. But in many cases this turned out to be less true than they expected, so they hired more workers than they could profitably support, and are now correcting to an appropriate level.
The post-COVID economy has behaved extremely oddly, with simultaneous high inflation, continued supply chain issues, wage growth, low unemployment, etc. There is an expectation that consumer spending will substantially reduce causing a recession, which will negatively impact the earnings of most companies. The technology industry is historically faster to act to changing conditions than other industries, as reacting quickly is a competitive advantage. Thus many companies are acting based off their assumptions of coming economic difficulties, and reducing staff expenses is an attempt to remain profitable despite a potential reduction in revenue.
The post-COVID stock market has had particularly negative sentiment for technology companies, with the tech-heavy NASDAQ down -22% over the last year compared to -12% for the broader S&P500. This obviously makes their investors unhappy, as an investment in a tech company has recently been worse than an investment in a non-tech company. Thus tech companies are acting to bolster investor sentiment by reducing costs, which will make them more profitable in the near and mid term.