Delete deleted-posts permanently in bulk?

Sorry if that’s a stupid question …
Why with Discourse it is not possible to easily delete messages from the database?

3 Likes

Discourse uses a mechanism called a ‘soft delete’ which makes it easy to correct mistakes. It also allows to make a difference between ‘deleted for other users’ and ‘deleted for admins’.

It also maintains database integrity. What if a deleted post was quoted (before it was deleted) ? Then the quoting post has a reference to a post that does not exist any more. That could lead to all kinds of errors.

2 Likes

Thanks now i understand… the only problem is if there are a lot of deleted messages.

1 Like

It’s been discussed and requested before, more than once e.g.

While I understand that soft-delete maybe useful, after some period (e.g. 1 week or month) it would be nice if deleted things were actually removed to save space and also ensure legal compliance e.g because some thing illegal was posted, or personal details need removing for GDPR compliance.

Having to log on and run a rake task to replace the message with ‘this has been deleted’ is a bit naff.

4 Likes

Opss… sorry, coming from phpBB I thought it could be done.

1 Like

its now implemented

6 Likes

Best,

But how we do that ???

2 Likes

I’ve been playing with that this morning, and I can’t figure it out. :slightly_smiling_face: Any pointers?

1 Like

well, the setting needs to be set in admin > settings > security (currently hidden for some reason) and then each admin would need it to be granted like moderator.

Once Granted, it would appear in the post admin menu

3 Likes

Ah, that might be my stumbling block. I was looking for can_permanently_delete in the admin settings and couldn’t see it.

1 Like

The site setting is hidden because we do not encourage its use. That was developed for the cases when some sensitive information was posted and it must be scrubbed from the database completely. One more thing, that operation is not a bulk operation anyway.

Since it is a hidden site setting, the only way to enable it is from the console.

5 Likes

This is almost exactly what I need! Thank you.

How would I do the syntax so that it only covers all posts deleted before xxxx date?

1 Like

That would be Post.with_deleted.where("deleted_at < 'YYYY-mm-dd'").update_all(...)

7 Likes

Can you let us know how to enable it from the console?

1 Like

Here’s the instructions:

3 Likes

Feature Enabled from console but how i can delete deleted-posts ??? all posts present in deleted-posts

1 Like

I really am no expert on this, but it seems like there’s enough snippets of information in this topic to create the code you’re looking for.

The topic I linked at the start has the warnings from the creators in about this being uncharted territory, and you do it at your own risk. But they also provide the ‘destroy_all’ piece which seems useful, and suggest doing batches to start with.

That topic also has a couple of examples on how to target either posts or topics which (combined with the examples @RGJ has given on how to target different specific posts) should get you pretty close.

I have no experience with this though, so I’m afraid I can’t give it the greenlight as I’ve never done it. :man_shrugging:

2 Likes

In order to delete the old posts on our forum, I used this. It is the result of combining The proper way to completely delete hundred of topics via rails? and @RGJ’s suggestions.

I chose to do this in the end, as I really wanted to remove the data from the database to protect the privacy of users. Rewriting the text to “This post has been deleted” still leaves the edit history intact and relatively easily accessible, so not good enough.

As we had 20,000 topics to destroy, it took a while!

Topic.with_deleted.where("deleted_at < '2021-08-28'").limit(1000).destroy_all

As this left a lot of orphaned posts, I had to follow it up with:

Post.where('topic_id not in (select id from topics)').limit(1000).destroy_all
4 Likes

I imagen that your ./launcher cleanup afterwards also free’d up some space :slight_smile:

1 Like

Unfortunately, despite running as expected the posts table is still full of orphaned posts. The topics table has been cleared nicely. The posts arent reachable when I try and visit them with /t/topic_id, but this is likely due to not having a valid Topic ID. I don’t quite understand why they are still there in the table though.

Can anyone suggest a better way to clean up the posts table?

1 Like