# Any api to purge old deleted posts / uploads

**URL:** https://meta.discourse.org/t/any-api-to-purge-old-deleted-posts-uploads/73495
**Category:** Support
**Created:** [6 november 2017 om 08:59 UTC](https://meta.discourse.org/t/any-api-to-purge-old-deleted-posts-uploads/73495 "2017-11-06T08:59:10Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![adopilot](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/adopilot/32/105040_2.png) [@adopilot](https://meta.discourse.org/u/adopilot)
#### Post date: [6 november 2017 om 08:59 UTC](https://meta.discourse.org/t/any-api-to-purge-old-deleted-posts-uploads/73495/1 "2017-11-06T08:59:10Z")

</div>

Our discource is runing out of space on server.  
We did bulk deleted old posts whit uploads.  
But We did not gain any space.  
When I query table topics they only have` topics.deleted_by_id is not null`  
Uploads for these topics are still on server.

Is there any way to make this Uploads orphans so sidekiq can clean these?  
Or is there any way to PRUGE deleted topics so they do not consume space any more ?

I saw [auto purge uploads from old deleted posts](https://meta.discourse.org/t/auto-purge-uploads-from-old-deleted-posts/51306) , but one year has past since post maded whit no replays.  
So I started searching my way to release space on server

---

<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: [6 november 2017 om 10:46 UTC](https://meta.discourse.org/t/any-api-to-purge-old-deleted-posts-uploads/73495/2 "2017-11-06T10:46:25Z")

</div>

Any thoughts @zogstrip?

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [6 november 2017 om 22:35 UTC](https://meta.discourse.org/t/any-api-to-purge-old-deleted-posts-uploads/73495/3 "2017-11-06T22:35:57Z")

</div>

Not sure how you _bulk deleted_ the old posts/topics but this should fix it

```plaintext
# ssh into server
cd /var/discourse
./launcher enter app
rails db
DELETE FROM post_uploads WHERE post_id IN (SELECT id FROM posts WHERE deleted_at IS NOT NULL)

```

---

<div class="post-metadata">

### Author: ![adopilot](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/adopilot/32/105040_2.png) [@adopilot](https://meta.discourse.org/u/adopilot)
#### Post date: [7 november 2017 om 07:15 UTC](https://meta.discourse.org/t/any-api-to-purge-old-deleted-posts-uploads/73495/4 "2017-11-07T07:15:40Z")

</div>

> [@zogstrip](#):
>
> Not sure how you bulk deleted the old posts/topics but this should fix it

We did delete post from advanced search result page

 ![image](https://global.discourse-cdn.com/meta/original/3X/2/5/2561059b8167d6b89bc0294eb958030da59b36a2.png)  
 ![image](https://global.discourse-cdn.com/meta/original/3X/4/5/45718e620dad9bc965721e38af4d679f05987a15.png)

---

<div class="post-metadata">

### Author: ![adopilot](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/adopilot/32/105040_2.png) [@adopilot](https://meta.discourse.org/u/adopilot)
#### Post date: [7 november 2017 om 10:21 UTC](https://meta.discourse.org/t/any-api-to-purge-old-deleted-posts-uploads/73495/5 "2017-11-07T10:21:00Z")

</div>

Here is my tsql for deleting uploads

### Warning

This tsql delete uploads from post whic is not deletete but belong to topics whic is deleted.  
I do no why discource left undeleted posts when topic is deleted.  
But from my poin of view.  
If topic is deleted, then all posts shoud be deleted in topics. Not just first one.  
This script delete uploads for all posts which belong to deleted topic no matter if post is deleted or not.

```
delete from public.post_uploads
where id in (
SELECT 
	post_uploads.id
-- ,uploads.filesize
 -- ,posts.id
    --,topics.id
FROM 
  public.topics
  inner join public.posts on topics.id=posts.topic_id
  inner join public.post_uploads on posts.id=post_uploads.post_id
  inner join public.uploads on post_uploads.upload_id=uploads.id
WHERE 
	topics.deleted_by_id is not null
  )

```

* * *

I am not for sure which of sidekiq jobs need to be triggered , but I get released space after manual triggering next Workers in scheduler

- Jobs::CleanUpUploads
- Jobs::PurgeDeletedUploads
- Jobs::DirectoryRefreshDaily
- Jobs::DirectoryRefreshOlder

And called ` sudo ./launcher cleanup` over SSH.

---

<div class="post-metadata">

### Author: ![nathank](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nathank/32/290039_2.png) [@nathank](https://meta.discourse.org/u/nathank)
#### Post date: [27 augustus 2021 om 23:52 UTC](https://meta.discourse.org/t/any-api-to-purge-old-deleted-posts-uploads/73495/7 "2021-08-27T23:52:02Z")

</div>

## Call for Help!

Can a clever person walk me through combining:

> [@adopilot](#):
>
> ```plaintext
> SELECT 
> post_uploads.id
> -- ,uploads.filesize
> -- ,posts.id
> --,topics.id
> FROM 
> public.topics
> inner join public.posts on topics.id=posts.topic_id
> inner join public.post_uploads on posts.id=post_uploads.post_id
> inner join public.uploads on post_uploads.upload_id=uploads.id
> WHERE 
> topics.deleted_by_id is not null
> )
> 
> ```

### AND

> [@zogstrip](#):
>
> ```plaintext
> ./launcher enter app
> rails db
> DELETE FROM post_uploads WHERE post_id IN (SELECT id FROM posts WHERE deleted_at IS NOT NULL)
> 
> ```

to make an awesome orphaned uploads purger run from the console?

I note that when I use `rails db`, I’m asked for `Password for user discourse:`. What do I do with that?

---

<div class="post-metadata">

### Author: ![nathank](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nathank/32/290039_2.png) [@nathank](https://meta.discourse.org/u/nathank)
#### Post date: [13 september 2021 om 13:00 UTC](https://meta.discourse.org/t/any-api-to-purge-old-deleted-posts-uploads/73495/8 "2021-09-13T13:00:42Z")

</div>

To answer my own questions:

> [@nathank](#):
>
> Can a clever person walk me through combining:

Actually, this is now obsolete. At least for posts since the issue of not deleting posts of a deleted topic was fixed some time back.

Posts older than that will still need some sort of cute solution.

> [@nathank](#):
>
> I note that when I use `rails db` , I’m asked for `Password for user discourse:` . What do I do with that?

Use the DISCOURSE\_DB\_PASSWORD which is in app.yml

---

<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: [3 februari 2024 om 23:51 UTC](https://meta.discourse.org/t/any-api-to-purge-old-deleted-posts-uploads/73495/9 "2024-02-03T23:51:47Z")

</div>


