How to keep a moodle database clean and trim

August 22, 2011

updated 07-25-2020

If your database is becoming very large

“Help, my moodle database is huge!”

How to delete old records in a huge moodle database
How to clean moodle database tables.
How to keep a moodle database clean and trimmed
How to delete old log table records from a moodle database


Moodle has “Site administration” settings to

“Moodle keeps extensive logs of user activity. Eventually, however, the logs will become so large that they begin to clog your server. Limiting the length of time logs are kept for will reduce database table size.
“In Moodle 1.9 onwards, history tracking of changes in grades-related tables may be disabled by checking the disablegradehistory box.
“In Moodle 1.9 onwards, the length of time that the history of changes in grade-related tables is kept may be set (from 30 days to never).
“You may delete all entries (logs) from table mdl_log”
– moodle docs

# in our db, 9 out of 10 records in these 2 tables are “null” junk.
# this removes the 90% junk:

DELETE FROM mdl_grade_grades
WHERE finalgrade IS NULL ;
CHECK TABLE mdl_grade_grades ;
#
DELETE FROM mdl_grade_grades_history
WHERE finalgrade IS NULL ;
CHECK TABLE mdl_grade_grades_history ;

#

Keep the number of records in these tables trimmed down

mdl_logstore_standard_log   mdl_cache_text   mdl_sessions2   mdl_lesson_branch
only keep the most recent 30 days or whatever

DELETE FROM mdl_logstore_standard_log
WHERE timecreated < UNIX_TIMESTAMP(CURDATE() - INTERVAL 30 DAY);
CHECK TABLE mdl_logstore_standard_log ;

“… our moodle installation was getting extremely slow. Upon further inspection it was traced to the database which was reaching 99% cpu every time someone was hitting a course page. The reason being our mdl_cache_text table had slowly grown to over 1 million entries! … I noticed in cron.php there is a section to clear it. … do they just expect us to notice this table once it grows to an enormous size and bring the entire moodle system to its knees?” – by Etan, moodle forum
“Hello Etan,
I have exactly the same problem mentioned by you. I had no option but to empty the table manually. In less than 24 hrs. of my clearing the table manually, it has grown to almost 2 MB.” – by Manish, moodle forum

mdl_cache_text no longer exists? -2020

2020 mdl_event table
and mdl_backup_logs
No longer being used?

Comments are closed.

We try to post all comments within 1 business day