# Permanently deleting posts from the db for privacy reasons

**URL:** https://meta.discourse.org/t/permanently-deleting-posts-from-the-db-for-privacy-reasons/116588
**Category:** Support
**Created:** [May 1, 2019, 3:38pm UTC](https://meta.discourse.org/t/permanently-deleting-posts-from-the-db-for-privacy-reasons/116588 "2019-05-01T15:38:06Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Tim\_Jefferies](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tim_jefferies/32/113296_2.png) [@Tim\_Jefferies](https://meta.discourse.org/u/Tim_Jefferies)
#### Post date: [May 1, 2019, 3:38pm UTC](https://meta.discourse.org/t/permanently-deleting-posts-from-the-db-for-privacy-reasons/116588/1 "2019-05-01T15:38:06Z")

</div>

I want to nightly delete posts that are in an autoclosed status in a specific category on a regular basis for privacy reasons

as discussed here:

> [@How to reduce database size](https://meta.discourse.org/t/how-to-reduce-database-size/110204/3):
>
> I’d like to know how to do this as well. I want to create a category that auto deletes posts. The idea is to have a space where people can speak freely knowing that whatever they post will auto delete after a certain time i.e. for privacy reasons. I have a category set to auto close topics after 168 hours. Now I need to figure out how to delve into the DB (I expect a crontab task is the best way to do this) and delete any posts including uploaded images from the server that belong to this cat…

Would this be the rails command?

```plaintext
Topic.where(closed: true).where(category_id: <your category id here>).destroy_all

```

Is there any risk to my site if I was to run this command every night?

---

<div class="post-metadata">

### Author: ![Tim\_Jefferies](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tim_jefferies/32/113296_2.png) [@Tim\_Jefferies](https://meta.discourse.org/u/Tim_Jefferies)
#### Post date: [May 3, 2019, 1:16am UTC](https://meta.discourse.org/t/permanently-deleting-posts-from-the-db-for-privacy-reasons/116588/2 "2019-05-03T01:16:57Z")

</div>

So I went ahead and set this up and it’s working.

For anyone that’s interested I created a bash script that I run daily from cron:

```
#!/bin/bash
# Script to permanently delete discourse posts from a specific category that are closed

expect -c "

# This may be $ or # or something else dependent on host environment. Change accordingly.

set prompt \"root@ip-172-31-31-39-app:/var/www/discourse# \"
spawn sudo /var/discourse/launcher enter app
expect $prompt
send \"echo 'Topic.where(closed: true).where(category_id: 11).destroy_all' | rails c > /dev/null\r\"
send \"exit\r\"
"

```

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [August 23, 2022, 10:17am UTC](https://meta.discourse.org/t/permanently-deleting-posts-from-the-db-for-privacy-reasons/116588/4 "2022-08-23T10:17:23Z")

</div>


