Admin tools/wrench on small action posts

I remembered this post from a little while ago. In case anyone else can benefit from this in the future, here was my solution to this problem in the end!


Nuking Unwanted Small Action Posts

The rails script below is needed in order to do this - I’ve named it smalldelete.rb and placed it at /var/www/discourse/script while inside the container.

We also need to go through and make sure every small action post we want to destroy has the word delete placed in the actual post section, to differentiate them from what we might want to keep. Here is an example of what I mean with an Auto Bump post:

You can change that keyword for something else if needed by editing the p.raw.strip.downcase == "delete" line to another keyword.

Script for smalldelete.rb


class TopicCleaner
  def self.delete_small_actions(topic_id, dry_run: false)
    topic = Topic.find_by(id: topic_id)
    unless topic
      puts "Topic #{topic_id} not found"
      return
    end

    posts = topic.posts.where(post_type: Post.types[:small_action])
    to_delete = posts.select { |p| p.raw.strip.downcase == "delete" }

    if to_delete.empty?
      puts "No small action posts with 'delete' found in topic #{topic_id}"
      return
    end

    puts "Found #{to_delete.size} matching posts in topic ##{topic_id}"

    to_delete.each do |post|
      if dry_run
        puts "Would PERMANENTLY delete post ##{post.id} (created #{post.created_at}, by #{post.user.username})"
      else
        post.destroy()
        puts "🗑️ Permanently deleted post ##{post.id}"
      end
    end

    puts dry_run ? "Dry run complete — no posts deleted." : "Finished permanent cleanup of topic #{topic_id}"
  end
end

The script finds a topic by ID, and then checks every post in that topic to see whether it contains the defined keyword, and nothing else. If this is true, then it will prime that specific post for deletion.


Once the script is set up, we can go into the rails console and load it:

load Rails.root.join("script/smalldelete.rb")

then, finally, we can run

TopicCleaner.delete_small_actions(topic_id, dry_run: false)

The dry_run flag is also included to list exactly what would be permanently deleted, if desired.


Now of course, these changes are entirely aesthetic and make no difference in terms of content, as soft-deleted posts won’t show to non staff members anyway.

I’m a particular fan of making everything look nice and tidy however, so I can definitely see some cases where certain forum owners may want to make use of this!

1 Like