Google Sheets Script to Remove Duplicates

How-to basics · Updated November 23, 2026

The simplest Google Sheets script to remove duplicates is a custom function using UNIQUE(), but for a true script that deletes rows, use Google Apps Script with SpreadsheetApp and a loop. Below is a ready-to-run script that removes duplicate rows based on a chosen column, plus explanations of how it works and safer alternatives.

Why Use a Script Instead of Built-in Tools?

Google Sheets already has two built-in ways to handle duplicates: Data > Data cleanup > Remove duplicates (which deletes entire rows) and the UNIQUE() function (which returns a list of unique values). These work fine for one-off tasks. However, a script is useful when you need to:

  • Automatically remove duplicates on a schedule or when a form is submitted.
  • Process large datasets repeatedly without manual clicks.
  • Remove duplicates based on a single column while keeping other data intact.
  • Combine duplicate removal with other actions like sorting or formatting.

Scripts run in Google Apps Script, which is JavaScript-based and integrated with Sheets. They can be triggered manually, from a button, or on a time-driven basis.

How the Script Works: Step-by-Step

Here is a basic script that removes duplicate rows based on column A. It keeps the first occurrence and deletes later duplicates.

function removeDuplicates() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var data = sheet.getDataRange().getValues();
  var seen = {};
  var rowsToDelete = [];

  // Loop from bottom to top to avoid index shifting
  for (var i = data.length - 1; i >= 0; i--) {
    var key = data[i][0]; // Column A (index 0)
    if (seen[key]) {
      rowsToDelete.push(i + 1); // +1 because sheet rows are 1-indexed
    } else {
      seen[key] = true;
    }
  }

  // Delete rows, starting from the last to preserve order
  for (var j = 0; j < rowsToDelete.length; j++) {
    sheet.deleteRow(rowsToDelete[j]);
  }
}

To use it: open your sheet, go to Extensions > Apps Script, paste the code, save, and run the function removeDuplicates. The first time, you’ll be asked to authorize permissions. The script reads all data into memory, tracks which values have been seen, and deletes rows from the bottom up so that earlier deletions don’t shift row numbers.

How to Choose the Right Script for Your Data

Not all duplicate-removal needs are the same. Consider these factors before writing or using a script:

  • Whole row vs. single column: The example above checks only column A. To check the entire row, use key = data[i].join('|') to combine all columns.
  • Header row: If your data has a header, start the loop at i = data.length - 1 and end at i = 1 (skip index 0). Or use getRange(2, 1, data.length-1, data[0].length) to exclude the header.
  • Case sensitivity: The script treats “apple” and “Apple” as different. To ignore case, use key = String(data[i][0]).toLowerCase().
  • Blank cells: Blank cells are considered duplicates of each other. You might want to skip empty keys: if (key === '') continue;.

Common Problems and How to Fix Them

Users often run into these issues when using scripts:

  • “Authorization required” error: This is normal. The script needs permission to access your sheet. Click “Review permissions” and allow.
  • Script runs but nothing happens: Check that you’re on the correct sheet. Use getSheetByName('Sheet1') instead of getActiveSheet() if you’re not sure.
  • Rows get deleted incorrectly: If you loop from top to bottom, deleting a row shifts the indices and skips rows. Always loop from bottom to top, as shown.
  • Performance issues with large data: For thousands of rows, the script may be slow. Consider using setValues() to write back a filtered array instead of deleting rows one by one.

Alternative: Using UNIQUE() or Built-in Remove Duplicates

Before scripting, consider simpler options:

  • UNIQUE() function: In a new column, type =UNIQUE(A:A) to get a list of unique values from column A. This doesn’t delete anything; it creates a new list. To get unique rows, use =UNIQUE(A:C) for a range.
  • Data > Data cleanup > Remove duplicates: This removes duplicate rows from a selected range. It’s fast and doesn’t require code. It works on the entire row, not a single column.
  • Pivot tables: For analysis, a pivot table can show unique values without altering data.

Scripts are best when you need automation or custom logic. For a one-time cleanup, built-in tools are safer and faster.

Safety and Data Loss Precautions

Scripts that delete rows are permanent and cannot be undone with Ctrl+Z. Before running any script:

  • Make a copy of your sheet: File > Make a copy.
  • Or export a backup: File > Download > Microsoft Excel (.xlsx).
  • Test the script on a small sample first.
  • Consider using a script that writes to a new sheet instead of deleting from the original.

To write to a new sheet, you can use UNIQUE() in a formula or modify the script to collect unique rows and set them to a new range. This is safer because it doesn’t destroy data.

Frequently Asked Questions

Can I remove duplicates based on multiple columns? Yes. In the script, combine columns into one key. For example, var key = data[i][0] + '|' + data[i][1]; to check columns A and B. Or use data[i].join('|') for the whole row.

How do I run the script automatically? In the Apps Script editor, click the clock icon (Triggers) and add a time-driven trigger. You can run it daily, hourly, or on specific events like form submission.

Does the script work on Google Sheets mobile app? No. Apps Script runs in the browser or on Google servers, not on the mobile app. But you can trigger it from a desktop and the changes will reflect on mobile.

What if my data has a header row? Modify the loop to start at index 1 instead of 0. Or use getRange(2, 1, data.length-1, data[0].length) to read data excluding the header.

Is there a way to highlight duplicates instead of deleting? Yes. Use conditional formatting: select the range, go to Format > Conditional formatting, choose “Custom formula is” and enter =COUNTIF(A:A, A1)>1. This highlights duplicates without deleting.

Can I undo a script that deleted rows? No, scripts cannot be undone with Ctrl+Z. That’s why backing up is critical. However, if you accidentally deleted, you can use File > Version history to restore an earlier version of the sheet.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.