Union Carbide Corp. said yesterday it will lay off 4,000 workers, sell off $500 million in assets and shut down plants as part of a dramatic shake-up designed to boost the company's stock price and fend off a hostile corporate raider.

The embattled chemical firm, which is facing an avalanche of lawsuits over last year's poison gas disaster in Bhopal, India, and recent gas leaks in West Virginia, also said it will buy back 10 million shares of its stock and spend an additional $100 million to improve environmental protection and safety at its plants.

The move comes two days after the New Jersey-based GAF Corp. boosted its holdings of Carbide stock to 7.1 percent. Some industry analysts believe the New Jersey-based GAF, controlled by real estate magnate Samuel J. Heyman, has launched a corporate raid that could lead to a takeover of the company by GAF or other raiders.

"Heyman has got a gun pointed at their head," said Paul Leming, a chemical industry analyst with Kidder, Peabody. "They're scared to death somebody is going to make a run, take them over and then auction off the company . . . . This is called management preservation. They're looking out for their own necks."

But industry analysts also say a restructuring and consolidation of Carbide operations had been anticipated for some time. Even before its problems stemming from the Bhopal disaster, the Danbury, Conn.-based company had experienced disappointing earnings stemming from excessive overhead, outmoded plants and a bloated work force.

The seven-step program announced by Carbide Chairman Warren M. Anderson yesterday is expected to achieve $300 million in annual cost savings for the company but will result in a one-time charge of $990 million against 1985 pre-tax earnings and will lower Carbide's after-tax earnings by about $8 a share. Anderson said the program was designed "to enhance shareholder value and achieve new levels of performance in safety and environmental protection."

Among the steps announced were:

The reduction of 4,000 white-collar jobs that will shrink the company's domestic salaried staff 15 percent by early 1986. This will cost the company $70 million in severance pay and retirement inducements this year, but will result in annual savings of $250 million. Carbide spokesman Ralph Leviton said most of the reductions will be achieved through through early retirement.

*The shutdown of petrochemical and metals plants as part of a wider $865 million write-off of inventories and other assets. Carbide will also try to sell $500 million in "non-strategic businesses and assets," possibly including some of its uranium mining operations in the West. The company would not identify which operations are targeted for closure or sale, but industry sources speculated that its Puerto Rican petrochemical plant was a likely early candidate.

*The purchase of 10 million shares of its common stock, to be financed in part by the reversion of $500 million in surplus pension funds to the corporation. Carbide currently has $1 billion in surplus funds in a total pension program of $2.9 billion, according to Leviton.

The latter move, industry sources say, is most directly related to the company's defensive strategy against a potential takeover. The buyback of 10 million shares will shrink the company's 70 million shares by nearly 13 percent, making it more difficult for an outsider to acquire a controlling interest.

In addition, surplus pension funds are frequently looked to by outside raiders as a source of quick profits, one industry source noted.

Carbide also said it will spend $100 million on new environmental protection and safety programs on top of the $125 million the company is already spending for these purposes this year. This move was prompted by the Aug. 11 gas leak at the company's Institute, W. Va., plant that injured 136 nearby residents, including 31 who were briefly hospitalized.

Carbide's stock closed yesterday at 55 5/8, up 7/8.