Remove All Bookmarks from Google Doc
LLM for ad hoc document automation tasks

Search for a command to run...
LLM for ad hoc document automation tasks

No comments yet. Be the first to comment.
Vibe Video, thoughts of video production in agentic world

demo with sample codes

Inspirations from old days

recursion, complexity, probabilitistic argument, algorithmic algorithm, experimentation on million level dataset

GCP Conversational Agents and CloudRun

When you import certain versoin of docx docment into Google Doc, there could be a lot of bookmarks, making the UI look a rather messy. See some related but unresolved discussions a few year back.
Now that we have the quick solution.
Open "Extensions --> App Script". Copy and paste the below. Then execute the function.
function removeAllBookmarks() {
var doc = DocumentApp.getActiveDocument();
var bookmarks = doc.getBookmarks();
// Iterate through bookmarks in reverse order to avoid index issues
for (var i = bookmarks.length - 1; i >= 0; i--) {
var bookmark = bookmarks[i];
var element = bookmark.getPosition().getElement(); //get the element the bookmark points to.
console.log(bookmark);
if(element){ //check if element is valid.
try {
bookmark.remove();
} catch (e) {
console.log(e)
}
}
}
}
The code is generated by AI, with a bit post editing.
function removeAllBookmarks() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var bookmarks = doc.getBookmarks();
// Iterate through bookmarks in reverse order to avoid index issues
for (var i = bookmarks.length - 1; i >= 0; i--) {
var bookmark = bookmarks[i];
bookmark.removeFromDocument();
}
}
There was an error:
TypeError: bookmark.removeFromDocument is not a function
We feedback the error to AI and ask for a revision.
function removeAllBookmarks() {
var doc = DocumentApp.getActiveDocument();
var bookmarks = doc.getBookmarks();
// Iterate through bookmarks in reverse order to avoid index issues
for (var i = bookmarks.length - 1; i >= 0; i--) {
var bookmark = bookmarks[i];
var element = bookmark.getPosition().getElement(); //get the element the bookmark points to.
if(element){ //check if element is valid.
bookmark.removeFromDocument();
}
}
}
The trial looks a bit random. It did not analyze the root cause of "removeFromDocument is not a function" is likely a change of funtion name.
We take closer look at the bookmark object:
console.log(bookmark);
Here is the output:
{ toString: [Function],
remove: [Function],
getId: [Function],
getPosition: [Function] }
The solution is to change from removeFromDocument to remove.
Tada! It works.
The function worked on most bookmarks, but there were a few instances of the below error:
Exception: Unexpected error while getting the method or property remove on object DocumentApp.Bookmark.
I din't know the root case in the end. Those bookmarks seem invisible and do not interfere with my further editing, so I decide to let them go and use a try-catch to skip those errors.
That completes the solution.
The total time spent on the case is about 5min (excluding writing this blog). Model is Gemini 2.0.
In the past, this kind of occasional demand can drive me crazy. Open Internet search often leads to fragmented discussion, and putting together those materials is often time consuming.
AI's code generation capability gets us much faster on the ground works. Sometimes they have knowledge gap due to outdated documentation / unknown user execution environment. This part of troubleshooting is quick for an experienced programmer.
This case again demonstrates the Geek Efficiency Curve Updated. Human + AI collaboration boost efficiency of tasks that are too large to treat manually, and are too small to invest in solid engineering resources.