Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Google Sheets Script to Remove Images: Full Guide

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To remove images from a Google Sheet using a script, you must use the Google Apps Script editor to run a function that iterates through all over-grid images and deletes them. This is the only efficient way to clear a large volume of floating images that cannot be deleted via standard cell clearing or the delete key. This process requires a few lines of JavaScript-based code that interacts with the SpreadsheetApp service to identify and remove image objects from the active sheet.

What You'll Need

  • A Google account with access to the specific Google Sheet containing the images.
  • A desktop web browser (Google Apps Script editor is not available on mobile apps).
  • Basic permission to edit the spreadsheet (Editor or Owner access).
  • A few minutes to copy, paste, and authorize the script.

Step-by-Step

  1. Open Your Spreadsheet: Launch the Google Sheet from which you want to remove the images. Ensure you are on the specific tab (sheet) where the images are located, as the script will target the active sheet by default.
  2. Access the Script Editor: In the top menu bar, click on Extensions and then select Apps Script. This will open a new browser tab with the Google Apps Script editor. Note that menu labels may differ slightly depending on your region or account version.
  3. Clear the Default Code: You will see a default function called myFunction(). Delete all the existing code in the editor window so you have a completely blank script file.
  4. Insert the Removal Script: Copy and paste the following code exactly as written into the editor:
    function removeAllImages() {
      var sheet = SpreadsheetApp.getActiveSheet();
      var images = sheet.getImages();
      
      for (var i = 0; i < images.length; i++) {
        images[i].remove();
      }
      
      Browser.msgBox('All floating images have been removed.');
    }
  5. Save the Project: Click the floppy disk icon (Save) in the toolbar or press Ctrl + S (Windows) or Cmd + S (Mac). Give your project a name, such as "Image Remover," and click Rename.
  6. Run the Script: In the toolbar at the top of the editor, ensure that removeAllImages is selected in the dropdown menu next to the "Debug" button. Click the Run button (the play icon).
  7. Authorize Permissions: Because this script modifies your spreadsheet, Google will prompt you for authorization.
    • Click Review Permissions.
    • Select your Google account.
    • You may see a screen saying "Google hasn't verified this app." Since you wrote/pasted this script yourself, click Advanced and then click Go to Image Remover (unsafe) at the bottom.
    • Click Allow to grant the script permission to manage your spreadsheets.
  8. Verify the Result: Switch back to your Google Sheets tab. You should see a pop-up message stating that the images have been removed, and all floating images should now be gone.

Common Mistakes

One of the most frequent mistakes is attempting to use this script to remove images that are inserted inside a cell using the =IMAGE("url") formula. This script targets "over-grid" images—those that float on top of the cells and can be moved around freely. If your images are part of a formula, you must delete the cell content or the formula itself; the script will not see them because they are treated as cell values, not image objects.

Another common error is running the script while the wrong tab is active. The command getActiveSheet() tells the script to only look at the tab you are currently viewing. If you have images across five different tabs, you will need to navigate to each tab and run the script five times, or modify the script to loop through all sheets in the workbook.

Users often forget to save the project before clicking "Run." If you do not save, the editor may run a previous version of the code or throw an error stating that the function cannot be found. Always ensure the red dot on the file tab has disappeared before executing the code.

Lastly, some users panic when they see the "unverified app" warning during authorization. This is standard behavior for any script created within a personal account that has not been submitted to Google for a formal security review. As long as you are using the code provided above, it is safe to proceed via the "Advanced" menu.

Tips

If you have a massive workbook with dozens of tabs and thousands of images, you can modify the script to clear the entire workbook at once. Instead of getActiveSheet(), use getActiveSpreadsheet().getSheets() and wrap the removal logic in another loop. This prevents the tedious process of switching tabs manually.

To make the script easier to use in the future, you can create a custom menu button. By adding an onOpen() function to your script, you can add a menu item called "Custom Tools" to your Google Sheets toolbar. This allows you to run the image removal tool with two clicks without ever opening the Apps Script editor again.

Before running any script that deletes data, it is highly recommended to create a backup of your sheet. Go to File > Make a copy. Since scripts cannot be "undone" using the standard Ctrl + Z undo button, having a duplicate ensures you don't accidentally lose images you intended to keep.

If you only want to remove images from a specific area, you can add a conditional check within the loop to examine the image's anchor cell (using getAnchorCell()) and only call the remove() method if the image falls within a certain row or column range.

Troubleshooting

If the script runs without error but the images are still there, first check if the images are truly floating images or if they are embedded in cells. Right-click an image; if you see an option to "Put image in selected cell," it is a floating image. If you cannot right-click it or it behaves like text, it is a cell-embedded image and this script will not work.

If you receive a "Permission Denied" or "Authorization Required" error, ensure you have completed the full authorization flow. Sometimes the pop-up window is blocked by your browser's pop-up blocker. Check the address bar for a blocked pop-up icon and allow it for script.google.com.

If the script crashes with a "Timeout" error, it is likely because you have an extreme number of images (thousands) and the script is exceeding the maximum execution time for a single run. In this case, try deleting images in smaller batches or refreshing the page and running the script again to pick up where it left off.

If you see a syntax error like "Unexpected token," double-check that you copied the code exactly. Ensure no extra characters were added at the end of the lines and that all curly brackets {} are closed. A missing bracket is the most common cause of syntax failures in Apps Script.

Frequently Asked Questions

Can this script remove images created with the =IMAGE formula?

No. Images created via the =IMAGE function are considered cell values. To remove those, you must select the cells and press the Delete key or use a script that clears cell contents.

Will this script delete images on all tabs at once?

No, the provided script only targets the active sheet. You must be on the specific tab you wish to clean when you click Run.

Is it possible to undo the image removal?

No. Actions performed by Google Apps Scripts cannot be reversed using the Undo button. Always make a copy of your spreadsheet before running the script.

Do I need to know how to code to use this?

No. You only need to be able to copy and paste the provided code into the editor and follow the authorization steps.

Will this script affect the formatting of my cells?

No. The script only targets image objects that float over the grid. It does not change cell colors, borders, fonts, or any data contained within the cells.

Why does Google say the app is unsafe?

Google displays this warning for any script that isn't from a verified third-party developer. Since you are adding the code to your own account, it is safe to bypass this via the Advanced settings.