[plone.app.versioningbehavior] AttributeError: 'Removed' object has no attribute 'object'

We have this (historic) code for flushing the version history of content type objects:

        context = self.context

        purgepolicy = getToolByName(context, "portal_purgepolicy")
        repository = getToolByName(context, "portal_repository")

        old_keep = purgepolicy.maxNumberOfVersionsToKeep
        purgepolicy.maxNumberOfVersionsToKeep = 1

        if repository.isVersionable(context):
            try:
                obj, history_id = dereference(context)
                purgepolicy.beforeSaveHook(history_id, obj)
                if hasattr(obj.aq_inner.aq_base, "version_id"):
                    del obj.version_id
                self.messages = [("info", _(u"History is cleared."))]
            except Exception as e:
                self.messages = [("error", _(u"Something went wrong.") + " ({})".format(e))]
                logger.error("Error clearing version history of {}".format(context.absolute_url()), exc_info=True)

        purgepolicy.maxNumberOfVersionsToKeep = old_keep

which leads to the error below using "Edit" after previously clearing the history

Traceback (innermost last):
  Module ZPublisher.WSGIPublisher, line 156, in transaction_pubevents
  Module ZPublisher.WSGIPublisher, line 338, in publish_module
  Module ZPublisher.WSGIPublisher, line 256, in publish
  Module ZPublisher.mapply, line 85, in mapply
  Module ZPublisher.WSGIPublisher, line 62, in call_object
  Module plone.z3cform.layout, line 63, in __call__
  Module plone.z3cform.layout, line 47, in update
  Module plone.dexterity.browser.edit, line 58, in update
  Module plone.z3cform.fieldsets.extensible, line 65, in update
  Module plone.z3cform.patch, line 30, in GroupForm_update
  Module z3c.form.group, line 145, in update
  Module plone.app.z3cform.csrf, line 22, in execute
  Module z3c.form.action, line 98, in execute
  Module z3c.form.button, line 315, in __call__
  Module z3c.form.button, line 170, in __call__
  Module plone.dexterity.browser.edit, line 30, in handleApply
  Module z3c.form.group, line 126, in applyChanges
  Module zope.event, line 32, in notify
  Module zope.component.event, line 27, in dispatch
  Module zope.component._api, line 124, in subscribers
  Module zope.interface.registry, line 442, in subscribers
  Module zope.interface.adapter, line 607, in subscribers
  Module zope.component.event, line 36, in objectEventNotify
  Module zope.component._api, line 124, in subscribers
  Module zope.interface.registry, line 442, in subscribers
  Module zope.interface.adapter, line 607, in subscribers
  Module plone.app.versioningbehavior.subscribers, line 62, in create_version_on_save
  Module Products.CMFEditions.CopyModifyMergeRepositoryTool, line 336, in save
  Module Products.CMFEditions.CopyModifyMergeRepositoryTool, line 500, in _recursiveSave
  Module Products.CMFEditions.ArchivistTool, line 267, in prepare
  Module Products.CMFEditions.ModifierRegistryTool, line 135, in getReferencedAttributes
  Module plone.app.versioningbehavior.modifiers, line 116, in getReferencedAttributes
  Module Products.CMFEditions.CopyModifyMergeRepositoryTool, line 410, in retrieve
  Module Products.CMFEditions.CopyModifyMergeRepositoryTool, line 563, in _retrieve
  Module Products.CMFEditions.CopyModifyMergeRepositoryTool, line 635, in _recursiveRetrieve
  Module Products.CMFEditions.ArchivistTool, line 342, in retrieve
  Module Products.CMFEditions.ArchivistTool, line 501, in __getitem__
AttributeError: 'Removed' object has no attribute 'object'

Any idea?

Plone Foundation Code of Conduct