How can I remove a category from a community? Question
On page /categories
it's easy to create or edit a category, but I cannot find a way to delete a category again. Is the functionality not existing?
Following the setup guide my user is a global admin but also in the admin tools only a create category
exists.
2 answers
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:
- 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.
- remove required_tags, topic_tags, moderator_tags and category_post_types from the category(those are not cleaned up properly currently).
- 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.
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
0 comment threads
I believe there's no way to remove a category without direct database hackery. We didn't want to have a Meta category for Collab, but when I asked about nuking it people expressed concerns about clobbering one of the default ones (nobody was quite sure what would break). The category is here, but we set its access level so that only moderators can see it.
I also don't know what would happen to posts in a category you deleted -- even if the posts are first deleted, they still expect to be associated with a community. This would show up on profiles and in the "recent deleted posts" mod tool, for example. Deleting a category might break things there.
If you want to experiment, I would create a new category, create one post in it, get that post ID (stash it somewhere), and delete the category in the DB. If the user profile page breaks because of that post, you could assign it to a different category (with the same tag set) in the database -- this is why I said to hold onto the post ID.
0 comment threads