public virtual void DeleteDataForExecution(IExecutionEntity executionEntity, string deleteReason, bool cancel) { // To start, deactivate the current incoming execution executionEntity.Ended = true; executionEntity.IsActive = false; bool enableExecutionRelationshipCounts = IsExecutionRelatedEntityCountEnabled(executionEntity); if (executionEntity.Id.Equals(executionEntity.ProcessInstanceId) && (!enableExecutionRelationshipCounts || (enableExecutionRelationshipCounts && ((ICountingExecutionEntity)executionEntity).IdentityLinkCount > 0))) { IIdentityLinkEntityManager identityLinkEntityManager = IdentityLinkEntityManager; ICollection <IIdentityLinkEntity> identityLinks = identityLinkEntityManager.FindIdentityLinksByProcessInstanceId(executionEntity.ProcessInstanceId); foreach (IIdentityLinkEntity identityLink in identityLinks) { identityLinkEntityManager.Delete(identityLink); } } // Get variables related to execution and delete them if (!enableExecutionRelationshipCounts || (enableExecutionRelationshipCounts && ((ICountingExecutionEntity)executionEntity).VariableCount > 0)) { ICollection <IVariableInstance> executionVariables = executionEntity.VariableInstancesLocal.Values; foreach (IVariableInstance variableInstance in executionVariables) { if (variableInstance is IVariableInstanceEntity variableInstanceEntity) { IVariableInstanceEntityManager variableInstanceEntityManager = VariableInstanceEntityManager; variableInstanceEntityManager.Delete(variableInstanceEntity); if (variableInstanceEntity.ByteArrayRef != null && variableInstanceEntity.ByteArrayRef.Id is object) { ByteArrayEntityManager.DeleteByteArrayById(variableInstanceEntity.ByteArrayRef.Id); } } } } // Delete current user tasks if (!enableExecutionRelationshipCounts || (enableExecutionRelationshipCounts && ((ICountingExecutionEntity)executionEntity).TaskCount > 0)) { ITaskEntityManager taskEntityManager = TaskEntityManager; ICollection <ITaskEntity> tasksForExecution = taskEntityManager.FindTasksByExecutionId(executionEntity.Id); foreach (ITaskEntity taskEntity in tasksForExecution) { taskEntityManager.DeleteTask(taskEntity, deleteReason, false, cancel); } } // Delete jobs if (!enableExecutionRelationshipCounts || (enableExecutionRelationshipCounts && ((ICountingExecutionEntity)executionEntity).TimerJobCount > 0)) { ITimerJobEntityManager timerJobEntityManager = TimerJobEntityManager; ICollection <ITimerJobEntity> timerJobsForExecution = timerJobEntityManager.FindJobsByExecutionId(executionEntity.Id); foreach (ITimerJobEntity job in timerJobsForExecution) { timerJobEntityManager.Delete(job); if (EventDispatcher.Enabled) { EventDispatcher.DispatchEvent(ActivitiEventBuilder.CreateEntityEvent(ActivitiEventType.JOB_CANCELED, job)); } } } if (!enableExecutionRelationshipCounts || (enableExecutionRelationshipCounts && ((ICountingExecutionEntity)executionEntity).JobCount > 0)) { IJobEntityManager jobEntityManager = JobEntityManager; ICollection <IJobEntity> jobsForExecution = jobEntityManager.FindJobsByExecutionId(executionEntity.Id); foreach (IJobEntity job in jobsForExecution) { JobEntityManager.Delete(job); if (EventDispatcher.Enabled) { EventDispatcher.DispatchEvent(ActivitiEventBuilder.CreateEntityEvent(ActivitiEventType.JOB_CANCELED, job)); } } } if (!enableExecutionRelationshipCounts || (enableExecutionRelationshipCounts && ((ICountingExecutionEntity)executionEntity).SuspendedJobCount > 0)) { ISuspendedJobEntityManager suspendedJobEntityManager = SuspendedJobEntityManager; ICollection <ISuspendedJobEntity> suspendedJobsForExecution = suspendedJobEntityManager.FindJobsByExecutionId(executionEntity.Id); foreach (ISuspendedJobEntity job in suspendedJobsForExecution) { suspendedJobEntityManager.Delete(job); if (EventDispatcher.Enabled) { EventDispatcher.DispatchEvent(ActivitiEventBuilder.CreateEntityEvent(ActivitiEventType.JOB_CANCELED, job)); } } } if (!enableExecutionRelationshipCounts || (enableExecutionRelationshipCounts && ((ICountingExecutionEntity)executionEntity).DeadLetterJobCount > 0)) { IDeadLetterJobEntityManager deadLetterJobEntityManager = DeadLetterJobEntityManager; ICollection <IDeadLetterJobEntity> deadLetterJobsForExecution = deadLetterJobEntityManager.FindJobsByExecutionId(executionEntity.Id); foreach (IDeadLetterJobEntity job in deadLetterJobsForExecution) { deadLetterJobEntityManager.Delete(job); if (EventDispatcher.Enabled) { EventDispatcher.DispatchEvent(ActivitiEventBuilder.CreateEntityEvent(ActivitiEventType.JOB_CANCELED, job)); } } } // Delete event subscriptions if (!enableExecutionRelationshipCounts || (enableExecutionRelationshipCounts && ((ICountingExecutionEntity)executionEntity).EventSubscriptionCount > 0)) { IEventSubscriptionEntityManager eventSubscriptionEntityManager = EventSubscriptionEntityManager; IList <IEventSubscriptionEntity> eventSubscriptions = eventSubscriptionEntityManager.FindEventSubscriptionsByExecution(executionEntity.Id); foreach (IEventSubscriptionEntity eventSubscription in eventSubscriptions) { eventSubscriptionEntityManager.Delete(eventSubscription); } } }