diff --git a/dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/maintenance/jdbc/JdbcMaintenanceStore.java b/dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/maintenance/jdbc/JdbcMaintenanceStore.java index 8de9a47f6771..e6c712d7774c 100644 --- a/dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/maintenance/jdbc/JdbcMaintenanceStore.java +++ b/dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/maintenance/jdbc/JdbcMaintenanceStore.java @@ -186,6 +186,11 @@ public int deleteSoftDeletedProgramInstances() { String pmSelect = "(select id from programmessage where programinstanceid in " + piSelect + " )"; + String smsSelect = + "(select outboundsmsid from programinstance_outboundsms where programinstanceid in " + + piSelect + + " )"; + /* * Delete event values, event value audits, event comments, events, * enrollment comments, enrollments @@ -200,6 +205,12 @@ public int deleteSoftDeletedProgramInstances() { + pmSelect, "delete from programmessage_phonenumbers where programmessagephonenumberid in " + pmSelect, + + // delete SMS objects linked to PIs + "delete from outbound_sms_recipients where outbound_sms_id in " + smsSelect, + "delete from programinstance_outboundsms where outboundsmsid in " + smsSelect, + "delete from outbound_sms where id in " + smsSelect, + // delete comments linked to both PIs and PSIs "delete from programstageinstancecomments where programstageinstanceid in " + psiSelect, "delete from programinstancecomments where programinstanceid in " + piSelect, @@ -266,6 +277,11 @@ public int deleteSoftDeletedTrackedEntityInstances() { String psiPmSelect = "(select id from programmessage where programstageinstanceid in " + psiSelect + " )"; + String smsSelect = + "(select outboundsmsid from programinstance_outboundsms where programinstanceid in " + + piSelect + + " )"; + /* * Delete event values, event audits, event comments, events, enrollment * comments, enrollments, tei attribtue values, tei attribtue value @@ -281,6 +297,12 @@ public int deleteSoftDeletedTrackedEntityInstances() { + teiPmSelect, "delete from programmessage_phonenumbers where programmessagephonenumberid in " + teiPmSelect, + + // delete SMS objects linked to PIs + "delete from outbound_sms_recipients where outbound_sms_id in " + smsSelect, + "delete from programinstance_outboundsms where outboundsmsid in " + smsSelect, + "delete from outbound_sms where id in " + smsSelect, + // delete objects related to any message related to obsolete PIs "delete from programmessage_deliverychannels where programmessagedeliverychannelsid in " + piPmSelect,