Deleting all annotations from pdf through API

72 views
Skip to first unread message

Paul V

unread,
Jan 14, 2025, 7:35:17 AMJan 14
to zotero-dev
I have a collection of papers in my subfield that have been heavily annotated. I want to share my library with other users, though without my annotations or tags. My ideal workflow is copying my library every few months to a shared group library and then deleting all the tags and annotations through O(1) operations (not scaling with library size).

I was able to make a script with the Actions and Tags plugin to remove the tags, but I cannot figure out how to do it for annotations. Is there an API call like `item.removeTag(tagID)` but for removing attachments? Does anyone else have another idea of how to accomplish this task?

Apologies for my lack of knowledge. This is my first foray into SQL and Javascript.

-Paul

Below is the script to delete all tags:

`if (!item) {
return "[Remove Tags] item is empty";
}

let removedTagsCount = 0;

// Iterate through tags and remove all
item.getTags().forEach(tagObj => {
const tag = tagObj.tag;
    item.removeTag(tag);
    removedTagsCount++;
});

if (removedTagsCount > 0) {
return `[Remove Tags] Removed ${removedTagsCount} tag(s) from ${item.getField("title")}`;
} else {
return `[Remove Tags] No tags found on ${item.getField("title")}`;
}`

iseexuhs

unread,
Jan 14, 2025, 2:36:29 PMJan 14
to zotero-dev
In https://github.com/zotero/zotero/blob/main/chrome/content/zotero/xpcom/reader.js, the "onDeleteAnnotations" function may help.

onDeleteAnnotations: async (ids) => {
let keys = ids;
let attachment = this._item;
let libraryID = attachment.libraryID;
let notifierQueue = new Zotero.Notifier.Queue();
try {
for (let key of keys) {
let annotation = Zotero.Items.getByLibraryAndKey(libraryID, key);
// Make sure the annotation actually belongs to the current PDF
if (annotation && annotation.isAnnotation() && annotation.parentID === this._item.id) {
this.annotationItemIDs = this.annotationItemIDs.filter(id => id !== annotation.id);
await annotation.eraseTx({ notifierQueue });
}
}
}
catch (e) {
this.displayError(e);
throw e;
}
finally {
await Zotero.Notifier.commit(notifierQueue);
}
},

Dan Stillman

unread,
Jan 16, 2025, 9:23:39 AMJan 16
Use `Zotero.Item#getAnnotations()` on an attachment item to get its annotation items.

https://github.com/zotero/zotero/blob/8cfcdeb8e67151e60485e100622f07c76888a1e0/chrome/content/zotero/xpcom/data/item.js#L4145-L4151

And `await erase()` or `await eraseTx()` on the annotation item (depending on whether you're within a transaction) to delete it.

Most things you want to do to items are in item.js (with some, like `erase()`, inherited from dataObject.js).

- Dan
Reply all
Reply to author
Forward
0 new messages