Could not delete due to association with another object: ProgramStageInstance

Hi

I tried to delete a program from DHIS2 but I couldn’t. It keeps telling me “Could not delete due to association with another object: ProgramStageInstance”. Also, I tried to delete a stage in a tracker program but it tells me the same thing. Can anyone help me with this?

I am using DHIS2 version 2.34.1.

Do you have access to your database and the capacity to run queries?

Yes, I have full access to the database.

Deleting a program is not so simple because you may have multiple dependencies.
First, you need to remove any data associated to that program. That implies all TEIs, Program Stage Instances and Program instance. But these have probably dependencies with the audit tables. Also, there are links to program indicators, program rules, event reports, tables, charts…

For example, to delete program stage instances linked to that program, you can try the following (but my guess is that you will get new errors due to other associations), just replace programUID with the UID of the program you want to delete (be careful with delete queries :warning:

delete from trackedentitydatavalue 
inner join programstageinstance on trackedentitydatavalue.programstageinstanceid = programstageinstance.programstageinstanceid 
inner join programstage on programstage.programstageid = programstageinstance.programstageid 
inner join program on program.programid = programstage.programid 
where program.uid = 'programUID';

delete from trackedentitydatavalueaudit 
inner join programstageinstance on trackedentitydatavalueaudit.programstageinstanceid = programstageinstance.programstageinstanceid 
inner join programstage on programstage.programstageid = programstageinstance.programstageid 
inner join program on program.programid = programstage.programid 
where program.uid = 'programUID';

delete from programstageinstance
inner join programstage on programstage.programstageid = programstageinstance.programstageid 
inner join program on program.programid = programstage.programid 
where program.uid = 'programUID';
1 Like

@Wut_Yee this might be of helpful to you for the similar question you asked here

1 Like

I will try this, I hope it works. Thank you for your replay and your help.

Thank you for your replay. I will check it out.