Camunda 维护操作-删除历史数据

474 阅读2分钟

1、使用 HistoryService#deleteHistoricProcessInstance 方法:该方法用于删除指定的历史流程实例以及与之关联的所有历史数据,包括历史任务、历史变量、历史流程变量等。

2、使用 HistoryService#createHistoricProcessInstanceQuery 方法进行批量删除:该方法可以根据查询条件,删除符合条件的所有历史流程实例及相关历史数据。

3、配置历史数据时间范围(History Time To Live):可以通过配置该选项来自动删除指定时间范围之前的历史数据。可以在 Camunda 的配置文件(如 camunda.cfg.xml)或者 Spring 配置文件(如 applicationContext.xml)中进行配置。

4、手动清理数据库表:可以手动删除历史数据存储的数据库表中的数据。在删除数据之前,需要先备份相关数据以便需要时进行恢复。

注意,在清理历史数据时需要谨慎操作,以免误删重要数据。建议在进行清理操作之前先进行备份,并对清理操作进行详细记录,以便需要时进行追溯和恢复

配置清理:

设置之后每执行一次实例,生成的记录都会带有removal_time_一值,计算方式为开始时间+90天。

# camunda历史清理配置
camunda:
    bpm:
        generic-properties:
            properties:
            	# 历史记录级别设置
                history: full
                # 批量清理运行时间窗口:设置在每天20:00-22:00
                # historyCleanupBatchWindowStartTime: "20:00"
                # historyCleanupBatchWindowEndTime: "22:00"

                # 批量清理运行时间窗口:设置在周日18:00-22:00
                sundayHistoryCleanupBatchWindowStartTime: "18:00"
                sundayHistoryCleanupBatchWindowEndTime: "22:00"

                # 清理策略:基于移除时间
                historyCleanupStrategy: removalTimeBased

                # 基于移除时间清理策略,进一步基于开始/结束时间+TTL计算removal_time_
                historyRemovalTimeStrategy: start
                # historyRemovalTimeStrategy: end

                # TTL 生存时间 90天
                batchOperationHistoryTimeToLive: P90D

                historyTimeToLive: P90D

                # 用于历史清理的并行作业数,后期可以调小该数据
                historyCleanupDegreeOfParallelism: 4

                # 单次批量处理实例数
                historyCleanupBatchSize: 100

手动清理 , 删除前先 备份数据表。

  1. ACT_HI_*: 'HI’表示流程历史记录。 这些表包含历史数据,比如历史流程实例,变量,任务等,共18张表。
TRUNCATE act_hi_actinst;
TRUNCATE act_hi_attachment;
TRUNCATE act_hi_batch;
TRUNCATE act_hi_caseactinst;
TRUNCATE act_hi_caseinst;
TRUNCATE act_hi_comment;
TRUNCATE act_hi_dec_in;
TRUNCATE act_hi_dec_out;
TRUNCATE act_hi_decinst;
TRUNCATE act_hi_detail;
TRUNCATE act_hi_ext_task_log;
TRUNCATE act_hi_identitylink;
TRUNCATE act_hi_incident;
TRUNCATE act_hi_job_log;
TRUNCATE act_hi_op_log;
TRUNCATE act_hi_procinst;
TRUNCATE act_hi_taskinst;
TRUNCATE act_hi_varinst;
  1. 如果当前运行的工作流实例也不需要了,清理方案:ACT_RU_* 和 ACT_HI_* 清理RU的表是,会有外键关联, 可以考虑 drop table,再 create
TRUNCATE   act_ru_authorization    ;
TRUNCATE   act_ru_batch    ;
TRUNCATE   act_ru_case_execution   ;
TRUNCATE   act_ru_case_sentry_part ;
TRUNCATE   act_ru_event_subscr ;
TRUNCATE   act_ru_execution   ;
TRUNCATE   act_ru_ext_task ;
TRUNCATE   act_ru_filter   ;
TRUNCATE   act_ru_identitylink ;
TRUNCATE   act_ru_incident ;
TRUNCATE   act_ru_job  ;
TRUNCATE   act_ru_jobdef   ;
TRUNCATE   act_ru_meter_log    ;
TRUNCATE   act_ru_task ;
TRUNCATE   act_ru_task_meter_log;
TRUNCATE   act_ru_variable ;

TRUNCATE act_hi_actinst;
TRUNCATE act_hi_attachment;
TRUNCATE act_hi_batch;
TRUNCATE act_hi_caseactinst;
TRUNCATE act_hi_caseinst;
TRUNCATE act_hi_comment;
TRUNCATE act_hi_dec_in;
TRUNCATE act_hi_dec_out;
TRUNCATE act_hi_decinst;
TRUNCATE act_hi_detail;
TRUNCATE act_hi_ext_task_log;
TRUNCATE act_hi_identitylink;
TRUNCATE act_hi_incident;
TRUNCATE act_hi_job_log;
TRUNCATE act_hi_op_log;
TRUNCATE act_hi_procinst;
TRUNCATE act_hi_taskinst;
TRUNCATE act_hi_varinst;