How to delete all rows containing specific text within a column in Google sheets?

Supposing, you have a range of data in a google sheet, now, you would like to delete the rows based on cell values in a column. For example, I want to delete all rows which contain the text “Complete” in Colum C. This article, I will talk about how to solve it in Google sheets.

Delete all rows containing specific text within a column with script code


Delete all rows containing specific text within a column with script code

To remove all rows which include the specific text in a column, the following script code can do you a favor, please do as this:

1. Click Tools > Script editor, see screenshot:

doc delete rows containing 1

2. In the new opened code window, copy and paste the following code into the blank code window, see screenshot:

function deleteRows() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getSheetByName('delete containing');
  var r = s.getRange('C:C');
  var v = r.getValues();
  for(var i=v.length-1;i>=0;i--)
    if(v[0,i]=='Complete')
      s.deleteRow(i+1);
};

doc delete rows containing 2

Note: In the above code, “delete containing” is the sheet name containing the data you want to delete, “C:C” is the column with specific text you want to delete from, “Complete” is the specific text that you want to delete rows based on, please change them to your need.

3. Then save this code, and then click Run button in the code window to execute this script code, see screenshot:

doc delete rows containing 3

4. And all rows which containing the specific text “Complete” have been deleted at once, see screenshot:

doc delete rows containing 4


Delete all rows containing specific text in Excel worksheet:

If you want to remove all rows which contain a specific value in a column, the Kutools for Excel's Select Specific Cells feature can help you to select all rows matched the criteria, and then you just need to delete the rows at once.

doc delete rows containing 5

Kutools for Excel: with more than 300 handy Excel add-ins, free to try with no limitation in 30 days. Download and free trial Now!


The Best Office Productivity Tools

Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

  • Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
  • Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
  • Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
  • Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
  • Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
  • Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
  • Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
  • Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
  • More than 300 powerful features. Supports Office/Excel 2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial. 60-day money back guarantee.
kte tab 201905

Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
officetab bottom
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Julia · 1 months ago
    Figured it out, but having trouble making it work for larger sheets since it's exceeding maximum runtime. Basically, the data that was being returned for 'v' was an 'object' type so you had to JSON.stringify() it first in order to detect values.

    function deleteRows() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var s = ss.getSheetByName('REPLACE WITH NAME OF SHEET'); var r = s.getRange('C:C'); // Replace 'C:C' to be the column you want to replace like start:end var v = r.getValues(); for(var i=v.length-1;i>=0;i--) { if(JSON.stringify(v[i]).includes('REPLACE WITH THE CHAR YOU WANT TO REPLACE')) { s.deleteRow(i+1); } } };

  • To post as a guest, your comment is unpublished.
    Joha · 1 years ago
    Why use the comma operator in v[0,i] ?
  • To post as a guest, your comment is unpublished.
    DylJohBar · 1 years ago
    This seems to work only if a cell contains only the text specified. I want to delete rows with cells that contain other text including the text specific. I have a bunch of email addresses and want to be rid of rows containing Gmail addresses, therefore I'm looking for cells containing 'gmail.com'.

    No luck here.
    • To post as a guest, your comment is unpublished.
      josh@wedoworldwide.com · 1 years ago
      Replace this line: if(v[0,i]=='Complete')
      With this: if(v[0,i].indexOf('gmail') > -1)
  • To post as a guest, your comment is unpublished.
    ibrahem · 2 years ago
    Hello,
    Can modify the below code to delete the coloured row instead of text


    function deleteRows() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var s = ss.getSheetByName('delete containing');
    var r = s.getRange('C:C');
    var v = r.getValues();
    for(var i=v.length-1;i>=0;i--)
    if(v[0,i]=='Complete')
    s.deleteRow(i+1);
    };
  • To post as a guest, your comment is unpublished.
    ibrahem · 2 years ago
    Hello,
    Can I modify the below code to delete a coloured cell instead of specific text:


    function deleteRows() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var s = ss.getSheetByName('delete containing');
    var r = s.getRange('C:C');
    var v = r.getValues();
    for(var i=v.length-1;i>=0;i--)
    if(v[0,i]=='Complete')
    s.deleteRow(i+1);
    };
  • To post as a guest, your comment is unpublished.
    Martha · 2 years ago
    Does nothing at all, not sure how to fix this.