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
- Delete duplicate rows on a schedule or on form submit: paste the script below into Extensions › Apps Script, set the tab and the key column, run it once in preview, then add Triggers › Add trigger › removeDuplicateRows › Time-driven › Day timer (or From spreadsheet › On form submit).
- Never delete, just show a clean copy: on another tab,
=FILTER(Sheet1!A2:C, Sheet1!B2:B<>"", COUNTIFS(Sheet1!B2:B, Sheet1!B2:B, ROW(Sheet1!B2:B), "<="&ROW(Sheet1!B2:B))=1)keeps the first row for each email and updates by itself. - Stop duplicates being typed: use a data validation rule instead; see prevent duplicate entries.
| Method | When it runs | Changes the data? | Best for |
|---|---|---|---|
| Script on a trigger (this guide) | Daily timer, hourly, on form submit, or from a menu | Yes, deletes rows | Form responses, imports, shared sheets that collect repeats |
| Formula on another tab | Every time the source changes | No, it’s a live copy | Reports and lookups that must never show a repeat |
| Data validation | While someone types | No, it rejects or warns | Hand-typed lists; doesn’t see form rows or pastes reliably |
| Data › Data cleanup › Remove duplicates | Once, when you click it | Yes | One-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.
| Row | A: Timestamp | B: Email | C: Name | Duplicate? |
|---|---|---|---|---|
| 2 | 11/18/2026 9:02 | ana@example.com | Ana | |
| 3 | 11/18/2026 9:40 | ben@example.com | Ben | |
| 4 | 11/18/2026 10:15 | Ana@Example.com␣ | Ana R. | ✔ same email as row 2 (case, trailing space) |
| 5 | 11/19/2026 8:05 | cara@example.com | Cara | |
| 6 | 11/19/2026 8:30 | (no email) | blank key: left alone | |
| 7 | 11/19/2026 11:47 | ben@example.com | Benjamin | ✔ same email as row 3 |
| 8 | 11/20/2026 7:58 | (no email) | blank key: left alone | |
| 9 | 11/20/2026 9:12 | dev@example.com | Dev |
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:
- It keeps the first row and removes later rows whose values match “any previous row”.
- Rows with different letter case, formatting or formulas but the same values count as duplicates.
- The numbers in
BUILTIN_COLUMNSare sheet column numbers (2 = column B), even when the range doesn’t start in column A. Leave the brackets empty (removeDuplicates()) to compare whole rows. - It also removes duplicate rows hidden by a filter, and content outside the range isn’t touched, so the range here covers every column from A to the last used one.
- The reference doesn’t mention spaces. If Ana@Example.com␣ must match ana@example.com, run Data › Data cleanup › Trim whitespace first or use the full script below, which trims.
- Empty key cells are equal values, so two rows with no email can be treated as duplicates of each other and one of them removed.
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
- Open Extensions › Apps Script in the spreadsheet, replace the sample code with the script and save.
- Set
TABto the tab’s name, for example'Form Responses 1', andKEY_COLUMNSto 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. - Choose
KEEP = 'first'(the oldest row stays) orKEEP = 'last'(the lowest row stays, which is the newest when new rows are added at the bottom, as with form responses). - Leave
PREVIEW = true, and runremoveDuplicateRowsonce from the editor. Google asks you to approve access to the spreadsheet the first time. - 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.
- 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:
| Setting | Rows deleted | Rows left (Name column) |
|---|---|---|
KEEP = 'first' | 4 and 7 | Ana, Ben, Cara, (no email), (no email), Dev |
KEEP = 'last' | 2 and 3 | Ana R., Cara, (no email), Benjamin, (no email), Dev |
Make it run on its own
- Once a day: in the Apps Script editor add Triggers › Add trigger › removeDuplicateRows › Time-driven › Day timer and pick an hour. Google’s installable triggers documentation says the exact time may be slightly randomized within that hour.
- Every hour: run
installHourlyTriggeronce from the editor. It creates the trigger in code withScriptApp.newTrigger('removeDuplicateRows').timeBased().everyHours(1).create(). Run it only once, or you’ll get one trigger per run. - Whenever a form response arrives: add Triggers › Add trigger › removeDuplicateRows › From spreadsheet › On form submit. With
KEEP = 'last', a person who submits again replaces their earlier row. The form itself still keeps every response; only the sheet rows are removed. - From a menu: the
onOpenfunction adds a Cleanup › Remove duplicate rows menu the next time the spreadsheet is opened, so people who don’t open the editor can run it.
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
- Wrong tab name.
getSheetByNameneeds the exact name, including spaces and capitals; if it doesn’t match, the script stops with an error about a null sheet. - Wrong column numbers. 1 is column A, 2 is B, and so on, counted from the left edge of the sheet. Check the preview tab before you turn deleting on.
- Sorted sheets and KEEP = 'last'. “Last” means lowest on the sheet. If someone sorts the tab newest-first, the oldest row is at the bottom. Sort by the timestamp, or keep
KEEP = 'first'on a newest-first sheet. - Formulas next to form responses. Deleting a row deletes everything in it, including helper columns you added beside the responses. Keep calculations on another tab.
- Dates and numbers. The key is the cell value as text, so 123 typed as a number and as text match, but two dates only match if they are the same date and time.
- Near matches. The script trims and ignores case, nothing more. Gmail dots, phone formats and typos need a cleaner key; see duplicates that don’t match exactly.
- Several people editing. A scheduled run can delete a row while someone is typing in it. Pick a quiet hour for the Day timer, and keep a backup of important tabs (File › Version history also lets you restore an earlier version).
- Want the duplicates combined, not deleted? Deleting loses the information in the extra rows. To sum amounts or join notes first, see merge duplicate rows.
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.
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
- How to Find and Remove Duplicates in Google Sheets: Highlight, Count, Delete and Keep the Original (2026)
- How to Highlight Duplicates in Google Sheets: Conditional Formatting Formulas for One Column, Whole Rows, Repeats Only and Two Sheets (2026)
- How to Compare Two Columns or Two Sheets in Google Sheets to Find Matches and Duplicates (2026)
- Duplicates That Don’t Match Exactly in Google Sheets: Case, Extra Spaces, Gmail Dots, Phone Formats and Typos (2026)
- How to Remove Duplicates in Google Sheets but Keep the Latest, First or Highest Row per Key (2026)
- How to Prevent Duplicate Entries in Google Sheets: Data Validation Rules, Warnings and a Small Script (2026)
- How to Count Duplicates in Google Sheets: per Value, Extra Rows, Unique Counts and Why the Numbers Disagree (2026)
- How to Combine Two Lists or Several Tabs in Google Sheets Without Duplicates: VSTACK, UNIQUE, One Row per Key and Other Files (2026)
- How to Merge Duplicate Rows in Google Sheets: Sum the Numbers, Join the Notes and Fill the Blanks (2026)
- UNIQUE vs Remove Duplicates in Google Sheets: Live or Static, Letter Case, Spaces, Columns and Headers (2026)
- How to Remove Duplicates Within a Cell in Google Sheets: Comma-Separated Lists, Tags, Line Breaks and Letter Case (2026)
- How to Show Only the Duplicates in Google Sheets: Filter Duplicate Rows, List Repeated Values and Find Rows That Appear Once (2026)
- How to Find Duplicates Across Multiple Columns in Google Sheets: Rows That Match on Two or More Columns, and Values Repeated Anywhere in a Range (2026)
- How to Find Duplicates in Google Sheets but Ignore Blank Cells: Highlight, Count, List and Remove Without Empty Rows Getting in the Way (2026)
- How to Find Duplicate Transactions in Google Sheets: Double-Imported Rows, Invoices Paid Twice and Same-Amount Charges Within a Few Days (2026)
- How to Format Phone Numbers in Google Sheets: Strip Dashes and Spaces, Show (555) 010-1234, Handle +1 and Extensions, Then Find Duplicate Contacts (2026)
- How to Change Text Case in Google Sheets: UPPERCASE, lowercase, Proper Case Names, Sentence Case and Duplicates That Differ Only in Case (2026)