LOS ANGELES — “Magnum, P.I.” star Tom Selleck has agreed to pay the nearly $22,000 bill of a private investigator hired by a California agency in a probe that revealed truckloads of water were being illegally diverted to the actor’s ranch during an ongoing drought, officials said.

Officials from the Calleguas Municipal Water District filed a legal complaint against Selleck on July 6, which said a hydrant in their district was tapped to supply his ranch, which is located in a nearby jurisdiction.

A legal settlement between Selleck and the Calleguas district, which was formally approved Wednesday by directors of the agency, will prevent Selleck from drawing water from the district for use on his ranch in the Hidden Valley area northwest of Los Angeles.

Selleck, who is most famous for playing a Hawaii private investigator in the 1980s television show “Magnum, P.I.,” has agreed to pay the district’s nearly $22,000 bill to hire a private investigator, officials said.

The district determined that several times from 2013 to 2015 a water-tender truck filled from a hydrant within the Calleguas district and delivered that water to Selleck’s ranch, according to the legal complaint.

The dispute came to light as California endures a devastating four-year drought that has led to mandatory water cutbacks on cities and towns.

“Mr. Selleck previously paid for all the water that he utilized, which the Calleguas Municipal Water District acknowledged had not been stolen,” Marty Singer, an attorney for Selleck, said in a statement to the Los Angeles Times.

Singer did not return a call or email.

The metered hydrant was in Thousand Oaks, which is part of the Calleguas water district, and was installed for use at a construction site, said the city’s public works director, Jay Spurgin.

A construction company paid for the water, but city officials do not know whether some of it was diverted out of the district, Spurgin said.

It was unclear whether Selleck may have paid the construction company for use of the water, but Calleguas water district general manager Susan Mulligan said in a statement that whether or not he did was irrelevant.

“No one has a legal right to district water by simply paying a rate to another water user based on the volume of water questionably obtained,” Mulligan said.

Selleck now stars on the CBS police drama “Blue Bloods.”