How to Remove Duplicates Automatically in Google Sheets: Apps Script on a Daily Timer, on Form Submit or From a Menu (2026)

Google Sheets has no “remove duplicates automatically” switch. Data › Data cleanup › Remove duplicates cleans the rows that are there when you click it, and the next form response, import or copy-paste can bring the duplicates straight back. To keep a sheet clean without remembering to do it, you need something that runs on its own: a short Apps Script on a trigger, or a formula that shows a de-duplicated copy on another tab. This guide gives you a tested script that keeps either the first or the newest row, ignores letter case and stray spaces, previews before it deletes anything, and runs on a daily timer or every time a Google Form response arrives.

The short answer

MethodWhen it runsChanges the data?Best for
Script on a trigger (this guide)Daily timer, hourly, on form submit, or from a menuYes, deletes rowsForm responses, imports, shared sheets that collect repeats
Formula on another tabEvery time the source changesNo, it’s a live copyReports and lookups that must never show a repeat
Data validationWhile someone typesNo, it rejects or warnsHand-typed lists; doesn’t see form rows or pastes reliably
Data › Data cleanup › Remove duplicatesOnce, when you click itYesOne-off cleanups

The example

A signup sheet fed by a Google Form. Column B holds the email, and one person should appear once. Two people signed up twice: Ana the second time with a capital letter and a trailing space (shown as ␣), Ben the second time as Benjamin. Two rows have no email at all.

RowA: TimestampB: EmailC: NameDuplicate?
211/18/2026 9:02ana@example.comAna
311/18/2026 9:40ben@example.comBen
411/18/2026 10:15Ana@Example.com␣Ana R.✔ same email as row 2 (case, trailing space)
511/19/2026 8:05cara@example.comCara
611/19/2026 8:30(no email)blank key: left alone
711/19/2026 11:47ben@example.comBenjamin✔ same email as row 3
811/20/2026 7:58(no email)blank key: left alone
911/20/2026 9:12dev@example.comDev

The rows that should go are 4 (Ana again) and 7 (Ben again), or, if the newest answer should win, rows 2 and 3. The blank-email rows are not duplicates of anything; the script leaves rows with an empty key alone so you can fix them by hand.

The quick version: the built-in removeDuplicates()

Apps Script has the same command as the menu item, Range.removeDuplicates(). If your data is already tidy (no stray spaces), this is all you need:

// Remove duplicate rows on a schedule with the built-in removeDuplicates().
const BUILTIN_TAB = 'Sheet1';         // the tab to clean
const BUILTIN_COLUMNS = [2];         // sheet column numbers that define a duplicate: 1 = A, 2 = B, 3 = C ...

function removeDuplicatesBuiltIn() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(BUILTIN_TAB);
  const lastRow = sheet.getLastRow();
  if (lastRow < 3) return;   // header plus fewer than two rows: nothing to compare
  const data = sheet.getRange(2, 1, lastRow - 1, sheet.getLastColumn());
  const before = data.getNumRows();
  const after = data.removeDuplicates(BUILTIN_COLUMNS).getNumRows();
  console.log('Removed ' + (before - after) + ' duplicate row(s) from ' + BUILTIN_TAB);
}

What the Apps Script reference says about it, and what that means for you:

The full script: preview, keep first or newest, trim and ignore case

This version builds its own key from the columns you choose, trimmed and lowercased, so Ana@Example.com␣ and ana@example.com match. It can keep the newest copy instead of the oldest, skips rows whose key is blank, and has a preview mode that lists the rows it would delete on a tab called Duplicate preview instead of deleting them. It deletes whole sheet rows, so formulas and formatting in the rows that stay are untouched.

// Remove duplicate rows automatically: trims spaces and ignores letter case,
// keeps the first or the last copy, and previews before deleting anything.
const TAB = 'Sheet1';           // the tab to clean
const KEY_COLUMNS = [2];        // columns that define a duplicate: 1 = A, 2 = B ... e.g. [1, 3] for A + C
const HEADER_ROWS = 1;          // rows at the top to leave alone
const KEEP = 'first';           // 'first' keeps the oldest copy, 'last' keeps the newest
const PREVIEW = true;           // true: only list what would go on a "Duplicate preview" tab

function removeDuplicateRows() {
  const lock = LockService.getScriptLock();
  if (!lock.tryLock(30000)) return;          // another run is still working
  try {
    const ss = SpreadsheetApp.getActiveSpreadsheet();
    const sheet = ss.getSheetByName(TAB);
    const lastRow = sheet.getLastRow();
    const lastCol = sheet.getLastColumn();
    const count = lastRow - HEADER_ROWS;
    if (count < 2) return;
    const rows = sheet.getRange(HEADER_ROWS + 1, 1, count, lastCol).getValues();
    const keyOf = r => KEY_COLUMNS.map(c => String(r[c - 1]).trim().toLowerCase()).join('\u0001');
    const order = rows.map((_, i) => i);
    if (KEEP === 'last') order.reverse();
    const seen = new Set();
    const drop = [];
    for (const i of order) {
      const key = keyOf(rows[i]);
      if (KEY_COLUMNS.every(c => String(rows[i][c - 1]).trim() === '')) continue;  // blank key: leave it
      if (seen.has(key)) drop.push(i); else seen.add(key);
    }
    drop.sort((a, b) => a - b);
    if (PREVIEW) {
      const out = ss.getSheetByName('Duplicate preview') || ss.insertSheet('Duplicate preview');
      out.clearContents();
      const header = sheet.getRange(1, 1, 1, lastCol).getValues()[0];
      const list = [['Sheet row'].concat(header)].concat(drop.map(i => [i + HEADER_ROWS + 1].concat(rows[i])));
      out.getRange(1, 1, list.length, lastCol + 1).setValues(list);
      return;
    }
    // Delete from the bottom up, joining neighbouring rows into one deleteRows call.
    for (let k = drop.length - 1; k >= 0; ) {
      let start = drop[k], n = 1;
      while (k - n >= 0 && drop[k - n] === start - 1) { start--; n++; }
      sheet.deleteRows(start + HEADER_ROWS + 1, n);
      k -= n;
    }
  } finally {
    lock.releaseLock();
  }
}

// Optional: a menu so anyone with edit access can run it from the sheet.
function onOpen() {
  SpreadsheetApp.getUi().createMenu('Cleanup')
    .addItem('Remove duplicate rows', 'removeDuplicateRows')
    .addToUi();
}

// Optional: run this once to clean every hour instead of once a day.
function installHourlyTrigger() {
  ScriptApp.newTrigger('removeDuplicateRows').timeBased().everyHours(1).create();
}

Set it up

  1. Open Extensions › Apps Script in the spreadsheet, replace the sample code with the script and save.
  2. Set TAB to the tab’s name, for example 'Form Responses 1', and KEY_COLUMNS to the columns that define a duplicate: [2] for column B, [1, 3] for A and C together. The parts are joined with a separator that doesn’t appear in normal text, so AB + 12 never matches A + B12.
  3. Choose KEEP = 'first' (the oldest row stays) or KEEP = 'last' (the lowest row stays, which is the newest when new rows are added at the bottom, as with form responses).
  4. Leave PREVIEW = true, and run removeDuplicateRows once from the editor. Google asks you to approve access to the spreadsheet the first time.
  5. Open the Duplicate preview tab. Column A is the sheet row that would be deleted, followed by that row’s contents. On the example it lists rows 4 and 7.
  6. Duplicate the tab you’re cleaning (right-click the tab › Duplicate) so you have a copy, set PREVIEW = false, save and run it once more. Then delete the preview and backup tabs when you’re happy.

What each setting does on the example:

SettingRows deletedRows left (Name column)
KEEP = 'first'4 and 7Ana, Ben, Cara, (no email), (no email), Dev
KEEP = 'last'2 and 3Ana R., Cara, (no email), Benjamin, (no email), Dev

Make it run on its own

The script takes a LockService script lock, so if two form responses arrive at the same moment the second run waits up to 30 seconds and the two runs never delete rows at the same time.

Why not onEdit?

A simple onEdit script only runs when a person edits a cell. Google’s triggers documentation says script executions and API requests don’t cause triggers to run, and form responses come in through their own On form submit trigger, so onEdit misses exactly the rows that tend to be duplicated. For typed entries, rejecting the duplicate as it is entered is usually better than deleting it later; the prevent-duplicates guide has a tested onEdit script for that.

No script: a de-duplicated copy that updates itself

If you’d rather never delete anything, leave the raw tab alone and put this in A2 of a new tab (copy the header row into row 1). It keeps the first row for each email and recalculates whenever the source changes:

=FILTER(Sheet1!A2:C, Sheet1!B2:B<>"", COUNTIFS(Sheet1!B2:B, Sheet1!B2:B, ROW(Sheet1!B2:B), "<="&ROW(Sheet1!B2:B))=1)

The running COUNTIFS counts each email only up to its own row, so the first copy gets 1 and later copies get 2 or more. COUNTIFS ignores letter case but not spaces, so on the example this shows 5 rows (Ana, Ben, Ana R., Cara, Dev): Ana’s second row stays because of its trailing space. After Data › Data cleanup › Trim whitespace on column B it shows 4. It also drops rows with a blank email; remove the Sheet1!B2:B<>"" part if you want to keep them. For a tab name with spaces, quote it: 'Form Responses 1'!B2:B. To keep the latest row per key, see remove duplicates but keep the latest row; for the trade-offs between a live formula and a one-time removal, see UNIQUE vs Remove duplicates.

Pitfalls

FAQ

Can Google Sheets remove duplicates automatically?

Not with a setting. Data › Data cleanup › Remove duplicates is a one-time command: it cleans the rows that are there when you run it, and new duplicates can arrive a minute later. To keep a sheet clean on its own you have three options: a small Apps Script that deletes duplicate rows on a daily timer or whenever a form response arrives, a formula on another tab that always shows a de-duplicated copy, or a data validation rule that stops duplicates being typed in the first place.

How do I remove duplicate Google Form responses automatically?

Paste the script from this guide into Extensions › Apps Script of the responses spreadsheet, set TAB to the responses tab and KEY_COLUMNS to the email column, set KEEP to 'last' if a resubmission should replace the earlier answer, test it with PREVIEW = true, then add the trigger Triggers › Add trigger › removeDuplicateRows › From spreadsheet › On form submit. The form itself still keeps every response; only the rows in the sheet are removed.

Why doesn't an onEdit script catch duplicates from a form or another script?

onEdit runs when a person edits a cell. Google’s Apps Script documentation says script executions and API requests don’t cause triggers to run, and form responses arrive through their own On form submit trigger. Use On form submit or a time-driven trigger for rows that are added by something other than a person typing.

Does Apps Script removeDuplicates() ignore letter case?

Yes. The Apps Script reference for Range.removeDuplicates() says rows with identical values but different letter cases, formatting or formulas are considered duplicates. It keeps the first row, also removes duplicate rows hidden by a filter, and leaves content outside the range alone. The column numbers you pass are sheet columns (2 = column B), not positions inside the range. The reference doesn’t say it ignores extra spaces, so trim the data first or use the longer script, which trims.

How do I keep the newest row instead of the first one?

In the script in this guide, set KEEP = 'last'. It reads the rows from the bottom up, keeps the lowest copy of each key and deletes the earlier ones. That is the newest row when new rows are added at the bottom, as with form responses. If your sheet is sorted some other way, sort it first or see the keep-latest guide, which picks the latest row by a date column.

How do I get deleted rows back?

Open File › Version history and restore or copy from an earlier version. Before you switch a script from preview to deleting, duplicate the tab (right-click the tab › Duplicate) so you have a copy, and keep PREVIEW = true until the preview tab lists exactly the rows you expect.

Rather check duplicates before they’re deleted? SheetDupes (disclosure: we make it) is a Google Sheets add-on for duplicate rows. It doesn’t run on a timer: you open it when you want a cleanup, tick the columns that define a duplicate, preview the matching groups and highlight the extra rows without changing any values, and only then remove them (keeping the first or last row in each group) or merge each group into one row. It copies a backup tab before it removes or merges anything and ignores extra spaces and, optionally, letter case and punctuation. Preview and Highlight are free on any size sheet; there is a 14-day free trial with no credit card, and writes remain free after the trial on sheets with up to 2,000 data rows during launch (through Jan 31, 2027; 500 after that). It only reads and writes the spreadsheet you have open, and your data is not sent to Loomwise. It is live on the Google Workspace Marketplace.
Install free from Google Workspace MarketplaceSee SheetDupes
Want to test it first? Try it on sample data (makes a copy of a 12-row example sheet in your Drive).

Related guides