A former Edgartown resident with a long criminal record pleaded guilty in Dukes County superior court Monday of failing to register as a sex offender.

Matthew P. Tucci, 32, now of Boylston, was sentenced to 238 days of time already served by the Hon. Cornelius J. Moriarty 2nd, an associate justice of the superior court.

The Cape and Islands District Attorney’s office declined to prosecute two other charges of heroin distribution, after the court ruled drugs seized when Mr. Tucci was arrested could not be admitted as evidence in court because police had improperly searched a backpack belonging to him.

Mr. Tucci, who is a convicted sex offender, was arrested in July 2014 on drug charges following an incident on Seaview avenue extension in Oak Bluffs. In October 2014 he was indicted by a Dukes County grand jury on both the drug charges and failure to register as a sex offender. On Jan. 20, Judge Moriarty ruled on a request to suppress drug evidence seized shortly after Mr. Tucci’s July 2014 arrest, when police seized a backpack from a nearby apartment where they believe Mr. Tucci was staying. In a later search of the backpack, police found syringes and 11 grams of heroin. Judge Moriarty ruled that police correctly determined the backpack belonged to Mr. Tucci, but he also found police had no legal right to search it and ruled the evidence inadmissible. Without that evidence, prosecutors declined to prosecute him on July 15 arrest charges. The prosecutor also declined to prosecute an earlier drug charge.

On Monday in the Edgartown courthouse assistant district attorney Laura Marshard and defense counsel Michael Cashman agreed on the plea, and offered it for the consideration of Judge Moriarty. Mr. Tucci was arrested on the charge shortly after his release from prison on a previous drug conviction.

“I will accept the recommendation, primarily based on the fact that he was only on the Island for a short time, and just got out of prison,” Judge Moriarty said from the bench. “It is with some reluctance. You have an awful criminal record for someone 32 years of age. It’s horrible. On this Island, you have been convicted of some very serious offenses.

Judge Moriarty made it clear that this is the last time Mr. Tucci would get the benefit of the doubt on the charge of failing to register as a sex offender, which carries a mandatory minimum sentence of five years for a subsequent offense.

“If you should fail to register again as a sex offender, I hope you understand, you’re going to go for five years,” the judge said.

In 2002, while living in Edgartown, Mr. Tucci was convicted in superior court on charges of rape of a 15-year-old girl, sentenced to five years in prison and ordered to register as a level three sex offender. He registered as a sex offender in Boylston, which he listed as his primary address. Under the law he is required to notify police in any other community, if he spends more than four days in any month there.

In court Monday, Ms. Marshard outlined the evidence against Mr. Tucci and recommended he be sentenced to time already served in the Edgartown jail.

“The commonwealth feels that is an appropriate resolution, give the amount of time he had been on the Island, a few days, prior to contact with police,” Ms. Marshard told the court. “It is well within the sentencing guidelines.”

Mr. Cashman acknowledged his client’s lengthy criminal record.

“Mr. Tucci tells me his is currently registered in Boylston,” Mr. Cashman said. “He is living with his dad. He’s had a long and checkered past. It’s a resolution we feel is fair to both the commonwealth and defendant.”

A second charge of failing to register as a sex offender was dismissed at the request of the commonwealth as part of the plea agreement in court Monday.