I also had to somehow delete an uploaded file. We don’t have the cleanup task enabled as some files come from an import from a different forum software and have not yet been referenced in imported posts correctly. So I needed to find a manual way. The following works but is not pretty …
-
Make sure the relevant upload is no longer in the current version of any post. That way, Discourse will consider it orphaned and not make trouble when you delete it.
-
Use the Data Explorer plugin or a different way to query the Discourse database to list orphaned uploads, find the relevant one, and note down its upload_id and filename. Relevant query:
SELECT uploads.id, uploads.user_id, uploads.created_at, uploads.url, uploads.filesize FROM uploads LEFT OUTER JOIN post_uploads ON uploads.id = post_uploads.upload_id WHERE post_uploads.post_id IS NULL ORDER BY created_at DESC LIMIT 100
-
In the database or with the Rails console for Discourse, delete the concerned record from table
uploads
by its upload ID. Here I use the Rails console:Upload.where(id: 16384).first.delete
-
Delete the associated file incl. all optimized versions (if any, applies to images) from the file system via SSH. Note the wildcard added before the file extension to also catch optimized versions, which have a suffix here. Of course,
cd /path/to/discourse/shared/public/ find . -name 43adade7a4cc64426adb8232a56cb2c3b49fb7c9*.pdf -type f -delete