Skip to content

Instantly share code, notes, and snippets.

@kampar
Last active January 22, 2026 08:04
Show Gist options
  • Select an option

  • Save kampar/1a6442b752bac1c1141c63751da9f4d2 to your computer and use it in GitHub Desktop.

Select an option

Save kampar/1a6442b752bac1c1141c63751da9f4d2 to your computer and use it in GitHub Desktop.
remove all images within specified docId
function removeEverythingById() {
// 1. Target the specific document by ID
var docId = '1hhWG7ZplBhDk8NQuCD6xLBpnrWJt81UfurP-Nr9xzw0';
var doc = DocumentApp.openById(docId);
var body = doc.getBody();
// --- HELPER FUNCTION: Recursively cleans any element ---
function deepClean(element) {
// A. Remove Standard Inline Images (e.g. "In line with text")
try {
var images = element.getImages();
for (var i = 0; i < images.length; i++) {
images[i].removeFromParent();
}
} catch (e) {}
// B. Remove Floating/Positioned Images (e.g. "Wrap text")
// FIX: We must remove them via the paragraph, using the image ID.
if (element.getParagraphs) {
var pars = element.getParagraphs();
for (var p = 0; p < pars.length; p++) {
var posImages = pars[p].getPositionedImages();
for (var j = 0; j < posImages.length; j++) {
// The Fix: Get ID, then tell paragraph to remove that ID
var imgId = posImages[j].getId();
pars[p].removePositionedImage(imgId);
}
}
}
// C. Dig into Tables (Recursion)
if (element.getTables) {
var tables = element.getTables();
for (var t = 0; t < tables.length; t++) {
var rows = tables[t].getNumRows();
for (var r = 0; r < rows; r++) {
var cells = tables[t].getRow(r).getNumCells();
for (var c = 0; c < cells; c++) {
deepClean(tables[t].getRow(r).getCell(c));
}
}
}
}
// D. Dig into Lists (Recursion)
if (element.getListItems) {
var listItems = element.getListItems();
for (var l = 0; l < listItems.length; l++) {
deepClean(listItems[l]);
}
}
}
// --- EXECUTION ZONES ---
// Zone 1: Main Body
deepClean(body);
// Zone 2: Headers
var header = doc.getHeader();
if (header) deepClean(header);
// Zone 3: Footers
var footer = doc.getFooter();
if (footer) deepClean(footer);
Logger.log("Finished processing document: " + docId);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment