U.S. District Judge Charles R. Richey ruled yesterday that the federal government can carry out its plan to close a decrepit 800-bed downtown Washington shelter for the homeless, but not before relocating its residents and devising a long-range plan "to eliminate homelessness in the nation's capital."

Government officials immediately said they would try to close the shelter at 425 Second St. NW, by their announced deadline of Aug. 31. But there was no specific indication where the estimated 600 men and women who have been sleeping there on recent nights will be housed.

Mitch Snyder, the activist whose 51-day hunger strike last fall prompted President Reagan to agree two days before his reelection to turn the shelter into a "model" facility, said flatly, "We ain't going nowhere. We will not allow this shelter to be closed.

"I'm telling the government now, 'Don't send your goons in here to remove anyone. Don't try,' " Snyder warned at a sidewalk press conference in front of the rat- and vermin-infested shelter operated by his Community for Creative Non-Violence. He added that CCNV plans to appeal the ruling.

Richey's ruling essentially hinged on his assessment that the shelter is not a fit place for anyone to live.

Secretary of Health and Human Services Margaret M. Heckler said federal officials "will immediately pursue with Mayor Marion Barry . . . and others, arrangements for the transfer of the shelter residents to alternative sites."

The District, in the past, has steadfastly refused to intervene in the protracted CCNV dispute with the federal government over the renovation of the shelter. Yesterday, Charles Seigel, a city spokesman, said the District would help in the relocation effort, but said the federal government "must find a way to take care of the people once they close the building.

"Frankly, we don't think it's going to happen" by Aug. 31, Seigel said. "We don't think it's going to be a 12-day question. That's an understatement."

Seigel and Assistant U.S. Attorney Royce C. Lamberth, who defended the federal government in Snyder's suit to force the government to rehabilitate the shelter, said some of the homeless who live at the CCNV shelter might be transferred to private facilities throughout the District.

But Snyder said that "if they'd have had such an alternative to this building, we would have seen it."

The dispute over the shelter, which occupies a supposedly temporary structure built during World War II, centers on the extent of the renovations, with the government contending that Reagan's plan to create a "model physical shelter" calls for $2.7 million worth of work. Snyder and other activists said more extensive work is needed that would cost $10 million. Long negotiations between Snyder and federal officials over the work eventually ended in mutual recriminations.

While seeking to close the facility, the federal government agreed to spend the $2.7 million for the relocation of the homeless who have lived there.

The White House said yesterday that Reagan believes he has met his obligation to Snyder. "It is our understanding that Secretary Heckler and her people have tried their best to work with Mitch Snyder within the framework of the agreement," spokesman Rusty Brashear said. "It's most unfortunate that for some reason that process broke down."

Richey had previously ordered the federal government to justify its announced plan to close the shelter. In his 47-page opinion yesterday, he said the government had done so and concluded that the building "certainly does not provide a place now fit for human habitation."

The judge said the federal government was perhaps correct that homelessness is essentially a local problem. But Richey said, "That perception would not, however, justify closing this huge shelter for homeless persons without any governmental commitment to see that the residents are given adequate shelter before the winter begins."

Richey said, however, that if the government does not adequately "meet the entire needs of each and every resident" of the shelter, then CCNV could ask him to appoint a special master to oversee the relocation process.

The judge urged the District government to become actively involved in the relocation effort, but said he realized that "the problem of the homeless in this city and throughout the nation cannot be solved by the government alone. There is only enough money in the federal budget to scratch the surface.

"Nevertheless, as in the case of a ravaging flood, tornado, or hurricane, no less than the president of the United States should treat this as a national emergency, and call upon the captains of industry, health care professionals, members of the medical and psychiatric professions, and others skilled in job training and counseling in order that the full impact of the nation's resources can be brought to bear to eliminate this national disgrace," Richey said.