DEV: Add rake task to bulk delete posts (#31576)

This PR adds a destroy:posts rake task that can be used to hard-delete a list of posts. Useful for dealing with large amounts of spam that has been soft deleted and needs to go.

Notes:

Works on both non-deleted and soft-deleted posts. (We might want to change this to work on only soft-deleted posts?)
Works exclusively on post IDs. We can't mix topic and post IDs as they might clash, and we have no way of resolving that ambiguity.
Accepts either a rake-style array of IDs or, more conveniently, you can pipe the argument in through STDIN.
Added a confirmation step since it's a fairly destructive operation.
This commit is contained in:
Ted Johansson
2025-03-04 17:29:38 +08:00
committed by GitHub
parent 06a0108a52
commit 42c4427cb1
6 changed files with 113 additions and 0 deletions

View File

@ -50,3 +50,16 @@ task "destroy:categories" => :environment do |t, args|
puts "Going to delete these categories: #{categories}"
categories.each { |id| destroy_task.destroy_category(id, true) }
end
# Hard delete a list of posts by ID. Pass a comma
# separated list either as a rake argument or through
# STDIN, e.g. through a pipe.
#
# Example: rake destroy:posts[4,8,15,16,23,42]
# Example: cat post_ids.txt | rake destroy:posts
desc "Destroy a list of posts given by ID"
task "destroy:posts" => :environment do |_task, args|
post_ids = args.extras.empty? ? STDIN.read.strip.split(",") : args.extras
puts "Going to delete these posts: #{post_ids}"
DestroyTask.new.destroy_posts(post_ids)
end