# Updating topic count in categories after bulk delete & missing topics/posts

**URL:** <https://meta.discourse.org/t/updating-topic-count-in-categories-after-bulk-delete-missing-topics-posts/89355>\
**Category:** Support\
**Created:** [June 7, 2018, 1:03pm UTC](https://meta.discourse.org/t/updating-topic-count-in-categories-after-bulk-delete-missing-topics-posts/89355 "2018-06-07T13:03:53Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![Bryan\_Underwood\_CA](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bryan_underwood_ca/32/95427_2.png) [@Bryan\_Underwood\_CA](https://meta.discourse.org/u/Bryan_Underwood_CA)\
**Post date:** [June 11, 2018, 4:04pm UTC](https://meta.discourse.org/t/updating-topic-count-in-categories-after-bulk-delete-missing-topics-posts/89355/8 "2018-06-11T16:04:39Z")

</div>

**FIXED.**

[I deleted my other replies in this topic, since they were the wrong path]

I happened to find this idea by @simon

> [@Post is deleted and shows as hidden in topic, not marked as hidden or deleted in db](https://meta.discourse.org/t/post-is-deleted-and-shows-as-hidden-in-topic-not-marked-as-hidden-or-deleted-in-db/82216/2):
>
> Through the rails console you’ll need to search with something like `Post.with_deleted.where("topic_id = ? AND deleted_at IS NOT NULL", your_topic_id)`

This helped me to see that the `Post.find_each` method was not returning deleted posts. So when I went to re-import, my data it still existed. Now, before I re-import I run this for topics (and separately, posts):

```plaintext
require_relative '../discourse/config/environment'

cnt = 0
#Topic.find_each do |t| # only returns active topics
Topic.with_deleted.find_each do |t| # returns both active and deleted topics
  if t.id > 6 && t.user_id != -2 # excludes included system topics and Discobot user Greeting messages
    puts "Topic: #{t.id}, #{t.title}, #{t.user_id}"
    t.destroy
        cnt = cnt +1
  end
end
puts "Deleted #{cnt} active and deleted topics where t.id > 6 && t.user_id != -2"

```

---

_[View the full topic](https://meta.discourse.org/t/updating-topic-count-in-categories-after-bulk-delete-missing-topics-posts/89355)._
