code: def delete_orphaned_attachments(portal):
      
      attachments = api.search({"portal_type": "Attachment"})
      total = len(attachments)
      logger.info("Integrity checking %d attachments" % total)
      for num, attachment in enumerate(attachments):
          obj = api.get_object(attachment)
          # The method `getRequest` from the attachment tries to get the AR
          # either directly or from one of the linked Analyses. If it returns
          # `None`, we can be sure that the attachment is neither assigned
          # directly to an AR nor to an Analysis.
          ar = obj.getRequest()
          if ar is None:
              obj_id = api.get_id(obj)
              api.get_parent(obj).manage_delObjects(obj_id)
              logger.info("Deleted orphaned Attachment {}".format(obj_id))
docs: Delete attachments where the Analysis was removed
         https://github.com/senaite/senaite.core/issues/1025