Skip to content

Clearing the Database

Trey Hayden edited this page Jan 28, 2022 · 7 revisions

The time will inevitably come when there is simply "too much" data in the LRS. Exactly when you cross the "too much" line is up to your discretion, but we typically consider the 100,000 statement mark to be close.

There are two ways to accomplish this, but you should make sure the LRS has been paused during the procedure and restarted afterwards using:

sudo service lrs stop
(or)
sudo service lrs start

The Django Script

Using Django itself is probably the safest way to accomplish this.

Inside of ADL_LRS/adl_lrs/management/commands, there is a clear_models.py script that will attempt to clear out all data in the LRS app. Users themselves will not be affected. To run this, SSH into the LRS and do the following:

(cd to ADL_LRS)
> source ../env/bin/activate
> python manage.py clear_models

This option is hit or miss. If the LRS has <= 100,000 statements, then this might not be a problem. But, if the shell prints Killed then this method will not work.

The SQL Script

The most performant way to clear data is to edit the SQL tables directly. As this method will require access to both the LRS and the Postgres DB machines, it's useful to have two shells open and SSH'd into each.

Note: this script will not clear out Agent entries. Those require a different script that excludes emails tied to LRS user accounts.

Create or place this script onto whichever machine contains the Postgres DB as clear.sql:

/* 
Script to clear out the ADL LRS
This script will remove all entries of xAPI data, leaving the Agents intact.
Those can be removed with another script.
*/
\echo "Clearing Statement Context objects ..."
DELETE FROM lrs_statement_context_ca_parent;
DELETE FROM lrs_statement_context_ca_category;
DELETE FROM lrs_statement_context_ca_grouping;
DELETE FROM lrs_statement_context_ca_other;

\echo "Clearing Sub-Statement objects ..."
DELETE FROM lrs_substatement_context_ca_parent;
DELETE FROM lrs_substatement_context_ca_category;
DELETE FROM lrs_substatement_context_ca_grouping;
DELETE FROM lrs_substatement_context_ca_other;
DELETE FROM lrs_substatement;

\echo "Clearing Statement Attachment objects ..."
DELETE FROM lrs_statementattachment;

\echo "Clearing Statements ..."
DELETE FROM lrs_statement;

\echo "Clearing xAPI components ..."
DELETE FROM lrs_verb;

DELETE FROM lrs_activitystate;
DELETE FROM lrs_activityprofile;

\echo "Clearing OAuth cache ..."
DELETE FROM oauth_provider_nonce;
DELETE FROM oauth_provider_consumer;
DELETE FROM oauth_provider_token;

\echo "Clearing Activities ..."
DELETE FROM lrs_activity;

\echo "Clearing hooks ..."
DELETE FROM adl_lrs_hook;

After you stop the LRS service (see above), you can then run this using:

> sudo –u postgres psql –d lrs –f clear.sql

Given the relational structure of the DB configuration, you may receive messages indicating that certain table entries could not be deleted because they are referenced in other tables. As those definitions cannot be recursive, the caveman approach is to simply continue running the script until those messages stop appearing.

Alternatively, we may need to just rearrange the deletion order to make it more elegant, but repeating the script will work.

Remember to restart the LRS service afterwards!

Clone this wiki locally