cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
dholton
Community Participant

Script for allowing users to remove themselves from courses?

Jump to solution

Colorado State seems to have a script that allows faculty to remove themselves from unneeded courses: How to remove yourself from unneeded courses - Canvas Information and Resources - Colorado State Uni... 

I contacted their support for more info but didn't get an answer.  Anyone have info on a script like this?  I'm currently in around 300 courses and growing and don't have permission to remove myself.  Others are in a similar boat.  The only docs I can find are for self-enroll courses, which provide an unenroll link.  I can remove others from courses, but not myself.  I made a Google sheet using the Canvas API to delete my enrollments but got a permission error.

Or is this something I should feature request?

Labels (1)
1 Solution
gtaylor
Community Contributor

here's a Windows Powershell script that will delete you from all of your courses.

As  @stuart_ryan ‌ says it's likly a filter is needed to not remove you from some courses.

The hard part seams to be getting permission to do the thing you want to do.

#sets permission to make api call
$token = "1234~sdkgffdjklfjfkdlgjklfsdjklgjklklfsdkljklgklfsdgklfdklgjklfsdklgg" #token Note this token is fake repalce it with your one or one that has permission to do the thing you want it to do.
$domain = "lorainccc.instructure.com" #change to your instution.
$headers = @{"Authorization"="Bearer "+$token}

#extra data
$date = Get-Date -Format "yyyy-MM-dd"
$logPath = "Y:\Canvas\Canvasfileload\logs\"

$userid = "sis_user_id:12345" #Replace 12345 use this if you know your instutional Canvas ID.
#or
#$userid = "12345" #Replace 12345 with your ID

#Api call for listing your enrollments Which gets us a list of course_IDs to remove from.
$listenrollmentapi = "/api/v1/users/$userid/enrollments?per_page=50"
$url = "https://$domain$listenrollmentapi"
$listenrollmentesults = Invoke-RestMethod -Headers $headers -Uri $url -Method GET

#reivew the enrollments and delete each one.
foreach($enroll in $listenrollmentesults)
{
$course_id = $enroll.course_id

$deleteenrollmentapi = "/api/v1/courses/$course_id/enrollments/$userid"
$url = "https://$domain$deleteenrollmentapi"
$deleteenrollmentresults = Invoke-RestMethod -Headers $headers -Uri $url -Method DELETE
}

View solution in original post

6 Replies
stuart_ryan
Community Coach
Community Coach

Hi  @dholton ,

I will admit I have not seen such scripts in action, though have certainly heard of them.

If you have attempted to use the API to remove yourself and have received a permissions error, then it is likely any such tool (as a rule) would result in the same issue. It sounds like your admins have not given you permission to remove yourself. This may be due to the enrolment coming from an upstream system which is automatically integrated, or it may simply be by choice.

I have had a look at the Colorado State University link you have provided, and it sounds to me like this is a custom utility that CSU has developed to achieve this. Interestingly, this would have been my final suggestion, which is that your institution could develop such a tool to permit governed self-unenrolling from courses, without giving out permissions to all teachers systemwide.

In this case, I would contact your institution and see what their recommendation is for self-unenrolling.

Hope that helps!

Stuart

(quick extra thought, I have assumed you are using an institution provided system, or would this be on the Canvas Free for Teachers service?)

dholton
Community Participant

Thanks, it's an institutional system.  At my last institution, it never was an issue because I had admin privileges and could just remove myself.

No worries! That makes sense. I know you can do a lot with the API as a user, however, I am unsure if you can unenroll yourself from a course. 

I think the cleanest solution would be something similar to what Colorado State did, as their tool would use a higher level of privileges to remove the users from the course at their request (rather than the user's own account permissions).

That would require it to be developed (or you may be able to contact Colorado State to see if they are willing to share/collaborate on the code).

Alternatively, I *anticipate* you would still need a higher level of admin permissions to remove yourself from a course, though I will have to test this and get back to you (unless someone can chime in earlier).

Please let me know how you go, or if you tinker with the API to give it a try yourself (I highly recommend a read over the Canvas APIs: Getting started, the practical ins and outs, gotchas, tips, and tricks‌ if you are starting out with the APIs).

Hope that helps!

Stuart

Boekenoogen
Community Contributor

Somekind of API script to mass remove an admin from multiple (or hundreds) of courses would be very useful.  We are in so many shells so to have a tool to pull us out in one shot would be great. 

Hi  @jrboek ,

That would certainly be doable, I haven't specifically seen one, but with admin permissions, you could write a script to do exactly that.

You would need to consider how you chose which ones to stay in and which ones to remove, that would add a layer of complexity. Though, all up, certainly a possibility.

Cheers,
Stuart

gtaylor
Community Contributor

here's a Windows Powershell script that will delete you from all of your courses.

As  @stuart_ryan ‌ says it's likly a filter is needed to not remove you from some courses.

The hard part seams to be getting permission to do the thing you want to do.

#sets permission to make api call
$token = "1234~sdkgffdjklfjfkdlgjklfsdjklgjklklfsdkljklgklfsdgklfdklgjklfsdklgg" #token Note this token is fake repalce it with your one or one that has permission to do the thing you want it to do.
$domain = "lorainccc.instructure.com" #change to your instution.
$headers = @{"Authorization"="Bearer "+$token}

#extra data
$date = Get-Date -Format "yyyy-MM-dd"
$logPath = "Y:\Canvas\Canvasfileload\logs\"

$userid = "sis_user_id:12345" #Replace 12345 use this if you know your instutional Canvas ID.
#or
#$userid = "12345" #Replace 12345 with your ID

#Api call for listing your enrollments Which gets us a list of course_IDs to remove from.
$listenrollmentapi = "/api/v1/users/$userid/enrollments?per_page=50"
$url = "https://$domain$listenrollmentapi"
$listenrollmentesults = Invoke-RestMethod -Headers $headers -Uri $url -Method GET

#reivew the enrollments and delete each one.
foreach($enroll in $listenrollmentesults)
{
$course_id = $enroll.course_id

$deleteenrollmentapi = "/api/v1/courses/$course_id/enrollments/$userid"
$url = "https://$domain$deleteenrollmentapi"
$deleteenrollmentresults = Invoke-RestMethod -Headers $headers -Uri $url -Method DELETE
}