Bulk job for editing pages per space

Hi all,

How do you user Script Runner to bulk edit all pages in space/s ?
What is the method which works for you ?

This is what I use:
** content = (content - MacroToRemove)
log.debug("Macro was removed from page: " + page)
page.setBodyAsString(content) //set the new content
page.setVersionComment(“Removal Job”)
pageManager.saveContentEntity(page, oldVersion, DefaultSaveContext.BULK_OPERATION)
**

I also configure:

Page oldVersion = page.clone() as Page
oldVersion.convertToHistoricalVersion()
oldVersion.setOriginalVersion(page)

I am trying to update page content (remove specific macro which exists on all pages in all spaces), but I encounter the following error:
2020-09-27 04:50:41,948 DEBUG [indigo.RemoveTargeted]: Exception = A different object with the same identifier value was already associated with the session : [com.atlassian.confluence.pages.Page#227246091]; nested exception is org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [com.atlassian.confluence.pages.Page#227246091]null
2020-09-27 04:50:41,949 DEBUG [indigo.RemoveTargeted]: Page: 227246089
2020-09-27 04:50:41,981 WARN [internal.LoadContexts]: HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@2828b8df<rs=com.mchange.v2.c3p0.impl.NewProxyResultSet@5f01b4ab [wrapping: null]>
2020-09-27 04:50:42,017 ERROR [common.UserScriptEndpoint]: *************************************************************************************
2020-09-27 04:50:42,019 ERROR [common.UserScriptEndpoint]: Script console script failed:
org.hibernate.HibernateException: Unable to resolve owner of loading collection [[com.atlassian.confluence.core.ContentEntityObject.contentProperties#227246089]] for second level caching
at org.hibernate.engine.loading.internal.CollectionLoadContext.addCollectionToCache(CollectionLoadContext.java:321)
at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:261)
at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:221)
at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:194)
at org.hibernate.loader.Loader.endCollectionLoad(Loader.java:1193)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1157)

When I run the script on single page it is working.

Thank you very much,
Simcha