How to Delete or Erase All Patients and Patient Data
This page is outdated and no longer receives updates!
What this page is for
These are the instructions for OpenMRS 1.6, OpenMRS 1.8.3, OpenMRS 1.9.8 and OpenMRS 2.1. They cover how to delete all of the patient data from OpenMRS while keeping all of the forms, users, and settings that you've made (i.e. this is how to clear or delete all patient information but keeping everything else).  It is also possible to do this by only exporting the tables that you want using mysqldump, but this is not covered here. Â
Documentation / How-To
To do this, you should log into your mysql database using a user that has delete privileges (best option is probably as an administrator).
For OpenMRS 1.6, paste the following queries in and you're done.
delete from obs;
delete from accesslogging_accesslog;
delete from encounter;
delete from patient_identifier;
delete from cohort_member;
delete from usagestatistics_usage;
delete from call_log;
delete from patient_program;
delete from sms_history;
delete from patient;
delete from person_address where person_id not in (select person_id from users);
delete from person_attribute where person_id not in (select person_id from users);
delete from person_name where person_id not in (select person_id from users);
delete from relationship;
delete from person where person_id not in (select person_id from users);
For OpenMRS 1.8.3, the command would need to take foreign keys into consideration and would look like this:
SET foreign_key_checks = 0;
delete from obs;
delete from accesslogging_accesslog;
delete from encounter;
delete from patient_identifier;
delete from cohort_member;
delete from usagestatistics_usage;
delete from call_log;
delete from patient_program;
delete from sms_history;
delete from patient;
delete from person_address where person_id not in (select person_id from users);
delete from person_attribute where person_id not in (select person_id from users);
delete from person_name where person_id not in (select person_id from users);
delete from relationship;
delete from person where person_id not in (select person_id from users);
delete from alert_archive;
delete from alert_error;
SET foreign_key_checks = 1;
Important Side Note:Â The first command disables foreign keys to allow deletion of the pt(patient tables) data. The second command deletes the pt data, but not user data, and the third command enables foreign keys again.
For OpenMRS 1.9.8, the command would need to take providers and visits into consideration and would look like this:
SET foreign_key_checks = 0;Â
TRUNCATE TABLE obs;Â
TRUNCATE TABLE encounter;Â
TRUNCATE TABLE accesslogging_accesslog;
TRUNCATE TABLE patient_identifier;Â
TRUNCATE TABLE cohort_member;Â
TRUNCATE TABLE usagestatistics_usage;
TRUNCATE TABLE call_log;
TRUNCATE TABLE patient_program;Â
TRUNCATE TABLE sms_history;
TRUNCATE TABLE patient;Â
delete from person_address where person_id not in (select person_id from users) AND person_id not in (SELECT person_id FROM provider);Â
delete from person_attribute where person_id not in (select person_id from users) AND person_id not in (SELECT person_id FROM provider);Â
delete from person_name where person_id not in (select person_id from users) AND person_id not in (SELECT person_id FROM provider);Â
delete from person where person_id not in (select person_id from users) AND person_id not in (SELECT person_id FROM provider);Â
TRUNCATE TABLE relationship; delete from person where person_id not in (select person_id from users) AND person_id not in (SELECT person_id FROM provider);Â
TRUNCATE TABLE alert_archive;Â
TRUNCATE TABLE alert_error;Â
TRUNCATE TABLE visit;Â
TRUNCATE TABLE visit_attribute;Â
TRUNCATE TABLE cashier_bill;Â
TRUNCATE TABLE cashier_bill_line_item;Â
TRUNCATE TABLE cashier_bill_payment;Â
TRUNCATE TABLE cashier_bill_payment_attribute;Â
TRUNCATE TABLE active_list;Â
TRUNCATE TABLE encounter_provider;Â
TRUNCATE TABLE idgen_log_entry;Â
TRUNCATE TABLE idgen_pooled_identifier;Â
TRUNCATE TABLE note;Â
TRUNCATE TABLE orders;Â
TRUNCATE TABLE patient_state;Â
TRUNCATE TABLE person_merge_log;Â
TRUNCATE TABLE test_order;Â
TRUNCATE TABLE xforms_person_repeat_attribute;Â
SET foreign_key_checks = 1;
Important Side Note: You can still use the 'delete from' syntax as previously, but 'TRUNCATE' is preferred which resets the auto-increment counters to 1.  Any commands dealing with tables that you don't have can be omitted; (for example tables added by modules you don't use).  You may have other tables added by modules that should also be cleared.
For OpenMRS 2.1, the command would need to take allergies into consideration and would look like this:
Â