Skip to content

Instantly share code, notes, and snippets.

@christopher-b
Created November 8, 2016 20:14
Show Gist options
  • Save christopher-b/072369296c4a6ea78b6f73edad5d278b to your computer and use it in GitHub Desktop.
Save christopher-b/072369296c4a6ea78b6f73edad5d278b to your computer and use it in GitHub Desktop.
Canvas - Delete export files
Attachment.where(workflow_state:'zipped').find_each do |attachment|
delete_attachment attachment
};nil
def delete_attachment(attachment)
# Don't delete parent Attachments
return if attachment.children.present?
# Delete associated ContentExports
content_export_for(attachment).try(:delete)
# Delete the db row. Trigger callbacks, then remove the row
attachment.destroy
attachment.delete
# Delete file on disk and containing folder, unless there is a parent Attachment
unless attachment.root_attachment_id
path = File.dirname(attachment.full_filename)
FileUtils.rm_rf(path) if File.directory? path
end
true
end
def content_export_for(attachment)
ContentExport.where(attachment_id:attachment.id).first
end
@mayng0702
Copy link

We are doing open source projects about canvas, may I have more information about the main class of attachment?

@christopher-b
Copy link
Author

Hi @mayng0702, for more information about the attachment class in Canvas LMS, check the main repo, specifically the Attachment model.

The concepts that are approached by this file (deleting unused files in Canvas LMS CV from disk) are elaborated in a module that can be found here.

@mayng0702
Copy link

mayng0702 commented Oct 9, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment