Deleting All Bookmarks in Folder (weird Fx bug?)

I’m trying to delete all bookmarks in a particular folder (used as a temporary storage area). Code appears to work fine but a weird bug occurs in certain situations. If the folder has been opened prior to the deletion and there was more than one bookmark in the folder, deleting the bookmarks via the add-on code results in the folder refusing to open and display the expected ‘Empty’ popup. A tiny line appears and it won’t open at all. (Hiding/re-displaying the Bookmarks Toolbar it’s on, or restarting Fx fixes the display issue.) I seem to remember encountering this same issue previously on some other add-on I had installed, and suspected this might be a Fx bug, but could not locate it in Bugzilla. Can someone confirm or know the Bug # or better yet know a workaround? Below is the code I am using; I’ve tried various methods including deleting, then creating a dummy one via code before removing it but so far no luck. Code is triggered via browser action toolbar button. (Removing the dummy works if done via 2nd browser action button click; obviously not ideal.) Thanks.

let itemsInFolder = await browser.bookmarks.getChildren(folder.id);
for (let item of itemsInFolder) {
  await browser.bookmarks.remove(item.id); 
}

Hi Tawn, a fan of your addon here!

Can you please clarify, opened how? Like in the Library view (Bookmarks → Manage Bookmarks)? “The expected ‘Empty’ popup” is not ringing a bell for me either.

Not sure whether it would fix anything, but perhaps as a side comment, I’d drop the await inside the for-loop – there shouldn’t be any problem deleting all of a folder’s child items simultaneously. If you need to wait for all deletions you’d do:

await Promise.all( 
  itemsInFolder.map(item => browser.bookmarks.remove(item.id))
);

Which add-on? LocBarUtil (webextension experiment on github) or Bookmark Tab Here (on AMO)?

To see what I mean by the ‘Empty’ popup:
Create a folder in bookmarks menu or bookmarks toolbar (do not add any bookmarks)
Click folder (as in attempt to open it)
‘Empty’ popup appears

The ‘Empty’ popup doesn’t appear in Library (Manage Bookmarks) or bookmarks sidebar because they have different UI.

IIRC, I added the await to see if that would fix bug. Perhaps I should be waiting for all deletions before/if adding new bookmarks, but it appears to make no difference, either overall or to this bug.

You can view the current state of the code at LocBarUtil/background.js at main · tawnC0DE/LocBarUtil · GitHub
But I’d kind of given up and added a delete/recreate action on the folder. There’s a very brief flash if you look closely, but not nearly as obvious as I’d feared. I’d still like to know if this is a Fx bug or something else.

Seemed like a Firefox bug to me. So I filed it: https://bugzilla.mozilla.org/show_bug.cgi?id=1980811

1 Like

Seems like a Firefox bug. I was able to reproduce the behavior described in the original issue in Firefox 141.0 on Windows 10. I couldn’t find any bug reports on BMO; I’d encourage you to file one :slight_smile:

In addition to the workaround @custom.firefox.lady described where you delete and recreate the folder, another option is to delete all of the bookmarks in the folder as originally described except the last one. For the last bookmark, if you move it to the folder’s parent then delete the bookmark, the folder be empty as expected.

const itemsInFolder = await browser.bookmarks.getChildren(folder.id);
for (const item of itemsInFolder) {
  if (item === itemsInFolder.at(-1)) {
    await browser.bookmarks.move(item.id, { parentId: folder.parentId });    
  }
  await browser.bookmarks.remove(item.id); 
}

EDIT: Oops, submitted to early.

Strangely when I added your code (followed by a ‘return;’ to skip the delete/recreate folder) I still triggered the bug. But at least I know it is a Firefox bug. I suppose the delete/recreate will have to do. Thanks.

@custom.firefox.lady, do you mean you added a return after the move() call? If so, that’s quite curious. Maybe there’s something about mixing moves and deletes that causes the folder to update correctly in a away that multiple deletes does not :thinking:

Anyway, happy to help :slight_smile:

The return is after the ‘for’ loop. Because I’m using the code I mentioned at LocBarUtil/background.js at main · tawnC0DE/LocBarUtil · GitHub and that code already has the delete/recreate folder workaround which your workaround would need to be tested without. Thus the temporary return to skip the original workaround for testing.

1 Like

Got it, thanks for the clarification.