The U.S. trade deficit shrank last month, according to figures released yesterday by the Commerce Department.

The preliminary figures showed the nation in the red on merchandise trade by $2.28 billion in June compared with a $3.96 billion trade gap on the same basis in May.

The June improvement came on both sides of the account, with a rise in exports and a drop in imports. U.S. exports rose by 5 1/2 percent in June to $18.64 billion after seasonal adjustments. Meanswhile, imports dropped to a seasonally adjusted $20.92 billion from the May total of $21.64 billion.

The import figures include the cost of insurance and freight, while exports are measured f.a.s. or "free alongside ship" which excludes the insurance and freight charges. Thus, the published deficit is bigger than the final figure, which will deduct the cost of insurance and freight from the import total.

Secretary of Commerce Philip Klutznick said yesterday that, "Viewed on a quarterly basis, our trade performance in the second quarter was the best since the fourth quarter of 1978."

As the economy plunged into recession in the second three months of the year, demand for imports fell. This was an important element in the trade improvement, but Klutznick said, "We take no joy in that part of the improvement which stems from the effect of the recession in reducing our import demand."

Oil imports dropped significantly in the second quarter of the year, contributing to the overall improvement in the trade balance. But Klutznick pointed out that these imports had "crept above the quarterly average" last month. Imports of petroleum and related products rose by 0.5 percent between May and June to $6.89 billion.

Prices for gasoline and other oil products have held steady or declined in recent weeks. The Commerce secretary cautioned that "this is no time to relax our vigilance," adding that "continuing stringent conservation efforts are essential" if the trade gap is not to be swelled again.

Manufacturing trade improved markedly in June, with the surplus rising from $364.9 million to $1.78 billion. Manufacturing exports rose by $665.4 million, while imports dropped by $750.4 million, in the month.

Between the first and second quarters oil imports fell by 15 percent in volume terms, to 6.8 million barrels a day.

The U.S. trade deficit with Japan narrowed to $863.5 million last month from $1.34 billion in May, while the surplus with Europe widened to $2.33 billion from $997 million in May.

Official forecast point to a trade deficit of $30 billion this year, and a spokesman for Commerce said yesterday that the latest figures suggest that it will be close to that. So far this year, exports are up 19 percent on an annual basis from their 1979 level, and imports are up 18 percent.