Scrub It Out! Erasing Sensitive Memorization in Code Language Models via Machine Unlearning

Zhaoyang Chu, Yao Wan, Zhikun Zhang, Di Wang, Zhou Yang, Hongyu Zhang, Pan Zhou, Xuanhua Shi, Hai Jin, David LoarXiv 2025 · DOI

Introduction

Code Language Models (CLMs), such as GitHub Copilot and Qwen2.5-Coder, have revolutionized software development by training on vast repositories of public code. However, these datasets often contain "secrets"—hardcoded API keys, private emails, and credentials. During training, models frequently exhibit verbatim memorization, allowing attackers or curious users to prompt the model to reveal these sensitive strings. This creates a significant privacy and security liability for model providers.

The Problem: The High Cost of Forgetting

Traditionally, addressing leaked secrets requires data scrubbing followed by full-model retraining. This process is prohibitively expensive, often costing hundreds of thousands of dollars and requiring between 900 to 1800 A100 GPU days. While techniques like Differential Privacy (DP) or data de-duplication can mitigate risks, they must be implemented during the initial training phase. Once a model is deployed, a more efficient solution is needed to handle "Right to Be Forgotten" (RTBF) requests.

Comparison of traditional privacy methods versus Machine Unlearning in terms of computational cost and workflow.

Methodology: Introducing CodeEraser

Researchers have developed CodeEraser, a surgical machine unlearning method designed to remove specific sensitive data without destroying the model's general coding utility. The authors compared three primary approaches:

  1. Vanilla Gradient Ascent (GA): Reverses the learning process for a sample, but often degrades the model's overall performance.
  2. Constraint-Based Unlearning (CU): Uses KL-divergence to keep the unlearned model close to the original, though it can still be too broad.
  3. CodeEraser (Selective Unlearning): This novel approach applies Gradient Ascent specifically to sensitive segments (the "secrets") while using Gradient Descent on the surrounding non-sensitive "context" to preserve structural understanding. A targeted KL-divergence constraint ensures the secrets become unpredictable rather than simply noisy.

Data and Insights

The study utilized a new Sensitive Memorization Dataset consisting of 50,000 high-risk samples confirmed to be memorized by models like CodeParrot and Qwen2.5-Coder. These samples include emails, IP addresses, and API keys identified via the detect-secrets tool.

Frequency distribution of memorized samples across different model families, illustrating the threshold for high-risk memorization.

One key insight is the 7% Rule: approximately 7% of training samples containing sensitive data are "high-risk" and easily extractable. Interestingly, the authors found that unlearning unique strings like API keys—which act as "noise" outliers—can actually improve model performance by allowing it to focus on representative code patterns.

Key Results

CodeEraser was evaluated on model families up to 7B parameters with impressive results:

  • Effectiveness: It reduced memorization in the Qwen2.5-Coder-7B model by 93.89%.
  • Utility Retention: The model maintained 99.99% of its original performance on benchmarks like HumanEval and MBPP. In contrast, vanilla unlearning methods often saw performance drops of nearly 30%.
  • Efficiency: CodeEraser processes a request in approximately 47 seconds per sample, a massive improvement over the months required for retraining.

Limitations and Future Work

While highly effective for individual or small-batch requests, CodeEraser's utility begins to decline when unlearning thousands of samples simultaneously. Additionally, the method's success is dependent on the accuracy of secret detection tools. Future research will focus on scaling to "mass unlearning" and testing on models exceeding 70B parameters.

Summary updated

BibTeX
@misc{chu2025scrub,
  author = {Zhaoyang Chu and Yao Wan and Zhikun Zhang and Di Wang and Zhou Yang and Hongyu Zhang and Pan Zhou and Xuanhua Shi and Hai Jin and David Lo},
  title = {{Scrub It Out! Erasing Sensitive Memorization in Code Language Models via Machine Unlearning}},
  year = {2025},
  eprint = {2509.13755},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2509.13755},
}
Figure