# Can't undelete category description posts created by deleted user

**URL:** https://meta.discourse.org/t/cant-undelete-category-description-posts-created-by-deleted-user/173696
**Category:** Bug
**Created:** [December 18, 2020, 11:36am UTC](https://meta.discourse.org/t/cant-undelete-category-description-posts-created-by-deleted-user/173696 "2020-12-18T11:36:01Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![jrivettcsa](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jrivettcsa/32/151272_2.png) [@jrivettcsa](https://meta.discourse.org/u/jrivettcsa)
#### Post date: [December 18, 2020, 11:36am UTC](https://meta.discourse.org/t/cant-undelete-category-description-posts-created-by-deleted-user/173696/1 "2020-12-18T11:36:01Z")

</div>

At some point after some categories were created, the user that created them was deleted. Looking at the log, I can see that Discourse deleted those posts automatically at the same time the user was deleted.

We failed to notice that this caused any problems until some time later, when it became clear that while admins can still see (and edit) the affected category description posts, when regular users try to view those posts, they get this instead: “Error; while trying to load; Something went wrong.”

These category description posts appear with red backgrounds. I can see that they were deleted, and when. I can see the ‘undelete’ button, but clicking it does nothing at all.

If I go to the settings for one of the affected categories (by clicking its Edit button), and click the Edit Description button, I’m taken to the corresponding deleted post. I can make changes and save them, and after I’ve refreshed the page, the new description appears. But non-admin users still can’t see the contents of the post (just that error).

I was able to change the ownership for one of the posts to my own admin user, but the undelete button still doesn’t do anything.

I’ve found similar reports on meta Discourse, but so far none of the advice helped.

I found one reference to a person who was able to undelete a similarly-affected post using the Discourse API, and I started looking into that, but I was unable to find anything about undeleting posts in the API documentation. See [Can't recover a topic by a deleted user](https://meta.discourse.org/t/cant-recover-a-topic-by-a-deleted-user/97048)

I hope there’s a solution that doesn’t involve recreating the affected categories from scratch.

Update: when I click the undelete button for one of these posts, it generates an entry in the error log:

````plaintext
Url: mysite/assets/ember_jquery-189e46ebcb33594b835e782fd1ce916ec750bc0cf980ebc4fb7796649161a18d.js
Line: 1
Column: 266460
Window Location: mysite/t/about-the-showcases-and-use-cases-category/28```
````

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [December 19, 2020, 2:23am UTC](https://meta.discourse.org/t/cant-undelete-category-description-posts-created-by-deleted-user/173696/2 "2020-12-19T02:23:07Z")

</div>

So someone issued a destroy command on the user who created the categories? Is there any particular reason this user was destroyed instead of anonymized?

It is true that the category description topic _will_ be owned by the user who creates the category, so destroying that user (and all their posts) could potentially cause this. @tshenry can we test this scenario next week?

---

<div class="post-metadata">

### Author: ![tshenry](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tshenry/32/119495_2.png) [@tshenry](https://meta.discourse.org/u/tshenry)
#### Post date: [December 19, 2020, 3:29am UTC](https://meta.discourse.org/t/cant-undelete-category-description-posts-created-by-deleted-user/173696/3 "2020-12-19T03:29:22Z")

</div>

I was able to run through it quickly just now and repro the same results.

1. Create a category with an admin user
2. Revoke their admin status and delete the user
3. The “About” topic for the category is soft-deleted when the user is deleted
4. Attempting to undelete/restore the topic doesn’t work
5. Changing ownership does technically work, but the topic remains in a strange deleted state no matter what you do from the UI.

From the console it looks like the `posts` record that is associated with the topic is deleted when the user is deleted. The topic record has a lot of 0’s when looking at various post related attributes.

It might be worth having an engineer see if we can be a bit more graceful here, but I suspect it’s easier said than done.

* * *

I was able to come up with a workaround to get back into a good place, but I had to use the command line. @jrivettcsa I would recommend taking a backup before you try this:

**Values needed**

- Category’s “About” topic ID  
Given [https://example.com/t/topic/1827](https://example.com/t/topic/1827), that would be `1827`

- Category’s ID  
Given [https://example.com/c/category-slug/5](https://example.com/c/category-slug/5), that would be `5`

Be sure to copy any of the content of the About topic you want to use later into a notepad or similar, then proceed with the following:

Enter the rails console

```plaintext
cd /var/discourse
./launcher enter app
rails c

```

Replace `<ABOUT_TOPIC_ID>` in the command below with the Category’s “About” topic ID, then run to hard-delete the broken topic.

```plaintext
Topic.where(id: <ABOUT_TOPIC_ID>).destroy_all

```

Press the q key to get back to the console if needed

The category is still associated with the bad topic, so we need to clear that out. Replace `<CATEGORY_ID>` in the command below with the Category’s ID, then run.

```plaintext
Category.where(id: <CATEGORY_ID>).update_all(topic_id: nil)

```

Press the q key to get back to the console if needed

Exit the rails console and run following rake task to create a new About topic for the category:

```plaintext
exit
rake categories:create_definition

```

Now that category should be fixed! You can rinse and repeat as needed.

---

<div class="post-metadata">

### Author: ![jrivettcsa](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jrivettcsa/32/151272_2.png) [@jrivettcsa](https://meta.discourse.org/u/jrivettcsa)
#### Post date: [December 19, 2020, 10:22am UTC](https://meta.discourse.org/t/cant-undelete-category-description-posts-created-by-deleted-user/173696/5 "2020-12-19T10:22:03Z")

</div>

Yes, some unfortunate changes were made by someone who should have known better. I cannot say more. Luckily these things are logged by Discourse.

---

<div class="post-metadata">

### Author: ![jrivettcsa](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jrivettcsa/32/151272_2.png) [@jrivettcsa](https://meta.discourse.org/u/jrivettcsa)
#### Post date: [December 19, 2020, 10:24am UTC](https://meta.discourse.org/t/cant-undelete-category-description-posts-created-by-deleted-user/173696/6 "2020-12-19T10:24:23Z")

</div>

Thanks for confirming the problem and providing a solution. I will attempt it, and post the results here.

---

<div class="post-metadata">

### Author: ![jrivettcsa](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jrivettcsa/32/151272_2.png) [@jrivettcsa](https://meta.discourse.org/u/jrivettcsa)
#### Post date: [December 19, 2020, 1:37pm UTC](https://meta.discourse.org/t/cant-undelete-category-description-posts-created-by-deleted-user/173696/7 "2020-12-19T13:37:28Z")

</div>

This solution worked perfectly. Thank you!

I fixed each one individually, but I think I could have done the `destroy_all` and `update_all` stuff in one rails console session, then issued the `rake categories:create_definition` command once at the end. What do you think? It might save somone a few minutes at some point.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [December 19, 2020, 9:37pm UTC](https://meta.discourse.org/t/cant-undelete-category-description-posts-created-by-deleted-user/173696/8 "2020-12-19T21:37:14Z")

</div>

It’s definitely a bug @jrivettcsa – when deleting a user can lead to a broken site, that’s bad! We’ll get it fixed.

---

<div class="post-metadata">

### Author: ![kris.kotlarek](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kris.kotlarek/32/176919_2.png) [@kris.kotlarek](https://meta.discourse.org/u/kris.kotlarek)
#### Post date: [December 23, 2020, 4:46am UTC](https://meta.discourse.org/t/cant-undelete-category-description-posts-created-by-deleted-user/173696/11 "2020-12-23T04:46:36Z")

</div>

Thank you for lodging that issue. I made a fix in this PR

[https://github.com/discourse/discourse/pull/11551](https://github.com/discourse/discourse/pull/11551)

When a user is deleted post describing category is not deleted anymore. Instead, the author of the post is changed from the original user to the system user.

---

<div class="post-metadata">

### Author: ![kris.kotlarek](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kris.kotlarek/32/176919_2.png) [@kris.kotlarek](https://meta.discourse.org/u/kris.kotlarek)
#### Post date: [December 23, 2020, 4:46am UTC](https://meta.discourse.org/t/cant-undelete-category-description-posts-created-by-deleted-user/173696/12 "2020-12-23T04:46:40Z")

</div>


