Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

#1: Initial revision by user avatar Taeir‭ · 2022-09-03T15:02:23Z (over 2 years ago)
To delete anything that cannot be deleted through the interface (if you really want it gone), I would recommend doing it in the rails console. Rails has support to indicate what to do with associated entities upon deletion, and some of the models have this specified for them. These rules define for example "when deleting a category also delete all posts associated with it" (cascading delete or in rails terms "dependent: :destroy") or "break all the links of the posts to the category by setting the category to NULL" (nullify).

Specifically for a category, it is not indicated on its associations. This means that deleting the category itself does not have a clearly defined cleanup procedure and will likely break things in the application (or the database may reject the deletion).

To "safely" delete a category:

1. First delete all its posts (that is, "destroy" them in the rails console). Posts do have all the deletion behavior set in the code, so they will clean up the associated votes, comments, etc.
2. remove required_tags, topic_tags, moderator_tags and category_post_types from the category(those are not cleaned up properly currently). 
3. Delete the category itself.

Only use this if you really want to delete all the data in the category, and make database backups if anything important is in there.

```ruby
RequestContext.community = Community.find_by(host: '<your community>')
cat = Category.find_by(name: '<your category>')
cat.posts.destroy_all
cat.category_post_types.destroy_all
cat.required_tags.destroy_all
cat.topic_tags.destroy_all
cat.moderator_tags.destroy_all
cat.destroy
```