How can I delete a topic permanently?

I want to delete a topic and its posts including uploaded files on this topic due to some legal regions. This topic is created by a forum user.
Is there any way for the same? Please help.

1 Like

Press the delete button on the topic.

If you need it removed from the database you will have to issue direct database commands; I believe we have a topic on this already somewhere perhaps @jomaxro?

1 Like

All posts related to this topics are deleted on discourse.
Please tell me any live link.

Not that I can see, no. We don’t generally recommend modifying the DB itself.


If you absolutely must delete a post completely, do the following.

  1. Take a backup. These commands are destructive, with no recovery path should you make a mistake.
  2. Note the topic ID. This is the number at the end of the URL, before the post number. In the example below, the topic ID is 289.
    image
  3. On your server, access the rails console, delete the posts, then the topic. Make sure to replace the topic ID in the example below.
    cd /var/discourse
    ./launcher enter app
    rails c
    Post.where(topic_id: 289).destroy_all
    Topic.find_by(id: 289).destroy!
    
8 Likes

Is there any way to delete all posts and uploads by a specific user?
I urgently need to delete all uploaded files by a user.

1 Like

Command doesn’t work for me.
image

You need to execute those in the Rails console. After ./launcher enter app, do rails c:

bionic-64-minimal ~ # cd /var/discourse/
bionic-64-minimal /var/discourse # ./launcher enter app
bionic-64-minimal-app:/var/www/discourse# rails c
[1] pry(main)> 
6 Likes

Whoops, I said “access the rails console”, but forgot to include the command :man_facepalming:. Updated. Thanks Bart!

2 Likes

Please note that the commands above do not delete the uploaded files, they just delete posts.
Here is the burn-with-fire way to do that. Use at your own risk.

User.where(:username => 'USERNAME').first.uploads.each do |upload|
  upload.optimized_images.each do |oi|
    FileUtils.rm(Discourse.store.path_for(oi))
    oi.destroy!
  end
  FileUtils.rm(Discourse.store.path_for(upload))
  upload.destroy!
end
3 Likes

Not working

image

Apparently some files are already missing. The code below will not give a fatal error on a missing file.

User.where(:username => 'USERNAME').first.uploads.each do |upload|
  upload.optimized_images.each do |oi|
    FileUtils.rm(Discourse.store.path_for(oi)) rescue nil
    oi.destroy!
  end
  FileUtils.rm(Discourse.store.path_for(upload)) rescue nil
  upload.destroy!
end
2 Likes

Commands worked perfactly but process stopped due to connection error.
Some files are still there.
Now when I am running it again, it shows following

That means it’s done, no more uploads to delete.

1 Like

But some uploads are still there.
Can we delete using upload id?