Retention Policy (Course Deletion) Script

chriscas
Community Coach
Community Coach
0
45

Hi All,

There have been a few posts in the Canvas LMS area asking about options for automatically deleting courses after and defined amount of time...   few years ago, I built this functionality into a python script that my institution runs on a daily basis to clean up some SIS things, ensure course options are set the way we'd like, etc...  Since there seemed to be some interest in this, I took a bit of time over the past couple weeks to uncouple this functionality from the rest of our big script so I could share it here.

I'm happy to say that you can now find my "retention policy processing" script on my Github.

The script was developed and tested with Python 3.19, but I believe it should work with 3.9 and higher...  To run this, you may need to install additional modules via pip depending what you already have with your python install.

Here's a quick overview of the script functionality.

  • The script is designed to be run once per day on a daily basis.
  • You'll enter some information about your Canvas instance (domain, admin API token, etc), along with email server info, and retention and notification period info in the top section of the script
  • The script will go through all of the terms in your Canvas instance, checking if the end date of the term (not individual course dates).
  • If the end date of the term is longer ago what the retention period which you set up, all courses in that term for the account_id you specify will be deleted (as uploaded, the retention policy is 7 years).  This is not a "hard delete", but rather the same type of delete that would happen if you clicked the "Delete this course" link in the UI, so the course could still technically be recovered and this should not change your overall storage space used.  As far as I know, this is the best option we have at the moment without going through Instructure.
  • If the term has no courses in any account after this action is taken, the term itself will be deleted.
  • If you define notification periods, the script will send out email messages to the teachers of the courses before they are deleted (as uploaded on Github, the script will send out notifications 60 days, 30 day, and 5 days before deletion).

I tried to use descriptive variable names and comment the code decently, but I also did not have a ton of time to dedicate to this, so apologies for any difficulty with readability.  I'm also a self-taught Python programmer, so I may not have done things in the most efficient way or in accordance with best practices, but my main focus was getting something that worked reliably.  Please feel free to use this script as a starting point, modifying to fit your needs.  I think it might be useful out of the box for some people, but I know there is a lot of variance on what schools would like to keep and how they setup Canvas courses, so this may not work at all for some (especially for those that do not use term dates).

I'm happy to try to answer questions about this script and correct any errors that may have slipped through my testing.  Please feel free to leave questions/comments here.

-Chris