Rails c, purge deleted posts (trick)

# Enter rails console:
cd /var/discourse/
./launcher enter app
rails c

# List all posts marked as deleted:
Post.with_deleted.where.not(deleted_at: nil)
# ... or count them:
Post.with_deleted.where.not(deleted_at: nil).count

# Then "purge" them with:
Post.with_deleted.where.not(deleted_at: nil).destroy_all
# ... or (to avoid verbosity):
Post.with_deleted.where.not(deleted_at: nil).destroy_all.count

:clap:

Also useful, delete (not just hide …) old posts revisions with SQL:

def execute_statement(sql)
  results = ActiveRecord::Base.connection.execute(sql)

  if results.present?
    return results
  else
    return nil
  end
end

and

execute_statement("delete from post_revisions *")
execute_statement("update posts set public_version=1 where public_version>1")
execute_statement("update posts set version=1 where version>1")
execute_statement("select * from post_revisions")

Edit: Rebake is not necessary.

outdated:

and rebake (outside rails c) (if necessary): rake posts:rebake

Admin/Mod user deletion:

u = User.find_by_username_or_email("ex@example.com")
u.admin = false
u.moderator = false
u.save
UserDestroyer.new(Discourse.system_user).destroy(u, delete_posts: false)

Another important point is:

Topic.with_deleted.where.not(deleted_at: nil).destroy_all.count
^^^^^