Remove unused assets from a course

Is there a way to clean up courses by removing CMS > Files & Uploads assets that have not been used in the course?

I’m looking for a Django management command to handle this if possible and avoid having to go through the course and delete these assets manually.

Is there a function that can look at a course in the contentstore() and see if an asset id is being used in the course?

I see that there is already a method to delete an asset.
edx-platform/cms/djangoapps/contentstore/asset_storage_handlers.py at master · openedx/edx-platform (github.com)

If we could limit the scope to just image assets (i.e. JPG, PNG) and look at the following modulestore.definition block_type (i.e. html, problem) then that would suffice for now.

I’m trying to clean up MongoDB fs.chunk size by deleting unused assets in courses and thinking that performing these actions would reduce MongoDB storage and backup costs.

cc @dave

I don’t believe any command currently exists for this.

The newer Files and Uploads page (I think it’s on by default in Redwood–not sure when it became available) hits a REST endpoint in Studio that looks something like:

/assets/{course-key}/{asset-key}/usage

That eventually calls into the same module that you linked, into the _get_asset_usage_path function. That being said, it looks like there are unhandled edge cases because it traverses into Units but wouldn’t find things that are in standalone XBlocks (like static tabs that people sometimes use to hold syllabi), or top level things like the course thumbnail and cert-related assets.

But if I were creating a new management command to do this, that’s where I would start.

1 Like

@dave
I was able to incorporate the endpoint that you mentioned to check asset usage and I’m seeing results. This helps a lot. Thank you.

I appreciate you informing me that this asset usage endpoint doesn’t currently check for edge cases like standalone XBlocks (i.e. static course tab), course thumbnail and cert-related usages. We definitely would need to consider adding a flag to exclude images like the course thumbnail from removal.

Shows where the iStock-508121319.jpg image is used in the course in multiple locations.

Shows where the AdobeStock_729677627.jpeg images is not used in the course.