delete from wee where exists 
(select 1 from wfe where wfe_id = wee_event_id 
and wfe_event_status in ('DONE','ERROR') 
and wfe_start_time < sysdate - 7)  ;

delete from wfe where wfe_event_status in ('DONE','ERROR') 
and wfe_start_time < sysdate - 7 ;
commit;

drop table temp_wfe;
create table temp_wfe as select * from wfe;
alter table twfl_wee_workflow_event_entry drop constraint wee_fk_0;

drop table twfl_wfe_workflow_event;

create table twfl_wfe_workflow_event
(
    wfe_cmp_id                    number(9,0),
    wfe_id                        number(9,0) not null,
    wfe_event_type                varchar2(50),
    wfe_event_status              varchar2(50),
    wfe_start_time                date,
    wfe_finish_time               date,
    wfe_acquire_lock              char,
    wfe_collaboration_area_id     number(9, 0),
    wfe_comment                   varchar2(3000),
    wfe_source_step_at_path_id    number(9, 0),
    wfe_dest_step_at_path_ids     varchar2(3000),
    wfe_prior_step_at_path_id     number(9, 0),
    wfe_deadline_step_at_path_id  number(9, 0),
    wfe_exit_value                varchar2(3000),
    wfe_user_id                   number(9, 0),
    wfe_error_message             varchar2(3000),
    wfe_row_order                 number(9, 0),
    constraint wfe_0_pk
        primary key (wfe_id)
) organization index tablespace indx pctthreshold 20 overflow tablespace users;

drop index iwfl_wfe_0;
create index iwfl_wfe_0 on twfl_wfe_workflow_event (
        wfe_event_status,
        wfe_id
) tablespace indx NOLOGGING;

drop synonym wfe;
create synonym wfe for twfl_wfe_workflow_event;

insert into wfe select * from temp_wfe; 

alter table twfl_wee_workflow_event_entry add constraint wee_fk_0
foreign key (wee_event_id) references twfl_wfe_workflow_event (wfe_id);
