# How do I delete the file of user avatar?

**URL:** <https://meta.discourse.org/t/how-do-i-delete-the-file-of-user-avatar/185321>\
**Category:** Product Support\
**Created:** [April 3, 2021, 4:00am UTC](https://meta.discourse.org/t/how-do-i-delete-the-file-of-user-avatar/185321 "2021-04-03T04:00:26Z")\
**Posts on this page:** 20\
**Page:** 1

## AI-generated summary

A user is trying to delete illegal user avatar images from their Discourse forum hosted in China to comply with local regulations. Despite replacing the avatars with default ones, the images remain accessible. The user attempts to delete them using the SHA1 hash and database queries but faces issues with certain avatars not being found on the server. The discussion reveals that the avatars might be cached in nginx, and after identifying the cache directory, the user successfully deletes the cached files, resolving the issue. [snakeninny](/t/-/185321/1), [pfaffman](/t/-/185321/2), [riking](/t/-/185321/6), [codinghorror](/t/-/185321/16), and [snakeninny](/t/-/185321/24) contributed to the solution.

<div class="post-metadata">

**Author:** ![snakeninny](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/snakeninny/32/115522_2.png) [@snakeninny](https://meta.discourse.org/u/snakeninny)\
**Post date:** [April 3, 2021, 4:00am UTC](https://meta.discourse.org/t/how-do-i-delete-the-file-of-user-avatar/185321/1 "2021-04-03T04:00:26Z")

</div>

Hi team,

My [website](https://iosre.com) is hosted in China and as you know all Chinese websites need to register for a license from the government, hence all Chinese websites are under supervision.

Someone recently reported to my CDN service provider, Qiniu, which is also a CN company, that some images on my website are illegal. My CDN provider informed me that I should delete these images and ‘refresh URL caches’, or they can suspend my account according to law. But I found these images were user avatars, which I don’t know how to delete.

According to [How to Delete Uploaded Files? - #3 by codinghorror](https://meta.discourse.org/t/how-to-delete-uploaded-files/25001/3) and [How to Delete Uploaded Files? - #25 by Falco](https://meta.discourse.org/t/how-to-delete-uploaded-files/25001/25) uploaded images w/o references will be auto deleted in 48 hours. I have replaced these user avatars with the default letter ones, but nothing seems to be happening to the old user avatars after 72 hours, maybe it’s because user avatars are not considered uploaded images?

Any ideas on how can I delete the user avatar files completely on my server?

Thanks in advance,  
Yinglu

---

<div class="post-metadata">

**Author:** ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
**Post date:** [April 5, 2021, 3:04pm UTC](https://meta.discourse.org/t/how-do-i-delete-the-file-of-user-avatar/185321/2 "2021-04-05T15:04:28Z")

</div>

I think it still works like this.

From the URL of the file you want to delete, get the filename.

```plaintext
cd /var/discourse/shared/uploads
find . -name =FILENAME= | xargs exec rm

```

And then you’d do whatever to purge it from your CDN.

---

<div class="post-metadata">

**Author:** ![snakeninny](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/snakeninny/32/115522_2.png) [@snakeninny](https://meta.discourse.org/u/snakeninny)\
**Post date:** [April 6, 2021, 3:25am UTC](https://meta.discourse.org/t/how-do-i-delete-the-file-of-user-avatar/185321/3 "2021-04-06T03:25:08Z")

</div>

Thanks for helping me, I tried but didn’t work ☹

The file is [https://iosre.com/user\_avatar/iosre.com/lincwee/135/5414\_2.png](https://iosre.com/user_avatar/iosre.com/lincwee/135/5414_2.png) so I guess the filename is 5414\_2.png, correct?

And the `/uploads` is under `/shared/standalone` rather than `/shared`  
And there’s no such a file under `/uploads`, as you can see in the shell:

```plaintext
root@iosre:/var/discourse/shared# ls
standalone
root@iosre:/var/discourse/shared# cd standalone/
root@iosre:/var/discourse/shared/standalone# ls
backups postgres_backup postgres_run state uploads
log postgres_data redis_data tmp
root@iosre:/var/discourse/shared/standalone# find . -name 5414_2.png
root@iosre:/var/discourse/shared/standalone# cd uploads/
root@iosre:/var/discourse/shared/standalone/uploads# find . -name 5414_2.png
root@iosre:/var/discourse/shared/standalone/uploads#

```

Any more ideas?

---

<div class="post-metadata">

**Author:** ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
**Post date:** [April 6, 2021, 1:18pm UTC](https://meta.discourse.org/t/how-do-i-delete-the-file-of-user-avatar/185321/4 "2021-04-06T13:18:03Z")

</div>

Oh sorry. Avatars work differently. I would have to look at the code to understand where to look. If this is an emergency and you have a budget please contact me directly. I likely won’t have free time to investigate otherwise. Perhaps someone else knows.

---

<div class="post-metadata">

**Author:** ![snakeninny](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/snakeninny/32/115522_2.png) [@snakeninny](https://meta.discourse.org/u/snakeninny)\
**Post date:** [April 7, 2021, 3:09am UTC](https://meta.discourse.org/t/how-do-i-delete-the-file-of-user-avatar/185321/5 "2021-04-07T03:09:21Z")

</div>

Sorry this is a personal website and I don’t have any budget for now. Thanks for your reply anyway!

---

<div class="post-metadata">

**Author:** ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)\
**Post date:** [April 7, 2021, 6:01am UTC](https://meta.discourse.org/t/how-do-i-delete-the-file-of-user-avatar/185321/6 "2021-04-07T06:01:35Z")

</div>

To prevent users from uploading future avatars, you can disable `allow uploaded avatars` and enable the `selectable avatars` site setting with a large selection.

> [@snakeninny](#):
>
> I have replaced these user avatars with the default letter ones, but nothing seems to be happening to the old user avatars after 72 hours, maybe it’s because user avatars are not considered uploaded images?

I think you may need to _upload a replacement placeholder_ in order for automatic deletion - the user record is still keeping the uploaded avatar image alive even though it’s not in use.

* * *

Upload files are named based on the SHA1 of the image content - that may help you to find the backing file?

---

<div class="post-metadata">

**Author:** ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
**Post date:** [April 7, 2021, 3:45pm UTC](https://meta.discourse.org/t/how-do-i-delete-the-file-of-user-avatar/185321/7 "2021-04-07T15:45:50Z")

</div>

Something like

```plaintext
a=UserAvatar.where(user_id: 1234)
u=Upload.find(a.custom_upload_id)
upload_url=u.url

```

Then

```plaintext
cd /var/discourse/shared/standalone
rm UPLOAD_URL_FROM_ABOVE

```

---

<div class="post-metadata">

**Author:** ![snakeninny](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/snakeninny/32/115522_2.png) [@snakeninny](https://meta.discourse.org/u/snakeninny)\
**Post date:** [April 8, 2021, 4:25am UTC](https://meta.discourse.org/t/how-do-i-delete-the-file-of-user-avatar/185321/8 "2021-04-08T04:25:09Z")

</div>

> [@riking](#):
>
> To prevent users from uploading future avatars, you can disable `allow uploaded avatars` and enable the `selectable avatars` site setting with a large selection.

I have over 7k registered users and only less than 5 users use illegal avatars w/o knowing it, so I don’t want to make it a 0 or 1 problem; I want to solve it case by case rather than stopping all users from using custom avatars.

> [@riking](#):
>
> I think you may need to _upload a replacement placeholder_ in order for automatic deletion - the user record is still keeping the uploaded avatar image alive even though it’s not in use.

How do I do this? I have no idea at all

---

<div class="post-metadata">

**Author:** ![snakeninny](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/snakeninny/32/115522_2.png) [@snakeninny](https://meta.discourse.org/u/snakeninny)\
**Post date:** [April 8, 2021, 4:29am UTC](https://meta.discourse.org/t/how-do-i-delete-the-file-of-user-avatar/185321/9 "2021-04-08T04:29:42Z")

</div>

> [@riking](#):
>
> Upload files are named based on the SHA1 of the image content - that may help you to find the backing file?

For example this image [https://iosre.com/user\_avatar/iosre.com/lincwee/135/5414\_2.png](https://iosre.com/user_avatar/iosre.com/lincwee/135/5414_2.png)

```plaintext
snakeninny@bogon ~ % shasum /Users/snakeninny/Desktop/5414_2.png.jpeg 
c8d561c5484a1f197abd32995411caaa25e53bd6 /Users/snakeninny/Desktop/5414_2.png.jpeg

```

```plaintext
root@iosre:~# cd /var/discourse/shared/standalone
root@iosre:/var/discourse/shared/standalone# find ./ -name *c8d561c5484a1f197abd32995411caaa25e53bd6*
root@iosre:/var/discourse/shared/standalone# 

```

No luck. Is this what you mean? And any ideas?

---

<div class="post-metadata">

**Author:** ![snakeninny](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/snakeninny/32/115522_2.png) [@snakeninny](https://meta.discourse.org/u/snakeninny)\
**Post date:** [April 8, 2021, 4:31am UTC](https://meta.discourse.org/t/how-do-i-delete-the-file-of-user-avatar/185321/10 "2021-04-08T04:31:36Z")

</div>

> [@pfaffman](#):
>
> ```plaintext
> a=UserAvatar.where(user_id: 1234)
> u=Upload.find(a.custom_upload_id)
> upload_url=u.url
> 
> ```

Is this a piece of code? Where and how do I execute it? I used to be an iOS developer and am not familiar with front-end programming

---

<div class="post-metadata">

**Author:** ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
**Post date:** [April 8, 2021, 12:59pm UTC](https://meta.discourse.org/t/how-do-i-delete-the-file-of-user-avatar/185321/11 "2021-04-08T12:59:49Z")

</div>

It’s for the rails console. You’d get there with

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

```

You can use `exit` to quit. And inside the container you cd to `/shared` rather than the full path above.

---

<div class="post-metadata">

**Author:** ![snakeninny](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/snakeninny/32/115522_2.png) [@snakeninny](https://meta.discourse.org/u/snakeninny)\
**Post date:** [April 8, 2021, 1:52pm UTC](https://meta.discourse.org/t/how-do-i-delete-the-file-of-user-avatar/185321/12 "2021-04-08T13:52:45Z")

</div>

For user `lincwee` in [https://iosre.com/user\_avatar/iosre.com/lincwee/135/5414\_2.png](https://iosre.com/user_avatar/iosre.com/lincwee/135/5414_2.png)  
`UserAvatar.where(user_id: 'lincwee')` returns an empty array. Any ideas?

```plaintext
root@iosre:~# cd /var/discourse/shared/
root@iosre:/var/discourse/shared# /var/discourse/launcher enter app 
WARNING: Docker version 17.05.0-ce deprecated, recommend upgrade to 17.06.2 or newer.
root@iosre-app:/var/www/discourse# rails c
[1] pry(main)> a=UserAvatar.where(user_id: 1234)
=> []
[2] pry(main)> a=UserAvatar.where(user_id: lincwee)
NameError: undefined local variable or method `lincwee' for main:Object
from (pry):2:in ` __pry__'
[3] pry(main)> a=UserAvatar.where(user_id: 'lincwee')
=> []
[4] pry(main)> 

```

---

<div class="post-metadata">

**Author:** ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
**Post date:** [April 8, 2021, 1:57pm UTC](https://meta.discourse.org/t/how-do-i-delete-the-file-of-user-avatar/185321/13 "2021-04-08T13:57:39Z")

</div>

You’ll need the ID, not username.

```
u=User.find_by(username: "lincwee")

```

You can then see the id or access it with `u.id`

---

<div class="post-metadata">

**Author:** ![snakeninny](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/snakeninny/32/115522_2.png) [@snakeninny](https://meta.discourse.org/u/snakeninny)\
**Post date:** [April 9, 2021, 2:16pm UTC](https://meta.discourse.org/t/how-do-i-delete-the-file-of-user-avatar/185321/14 "2021-04-09T14:16:56Z")

</div>

Thanks it worked to some extent. I’ve run

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

```

In the terminal and then executed the code

```plaintext
uid = User.find_by(username: "user_name").id
user_avatars = UserAvatar.where(user_id: uid)
user_avatar = user_avatars[0]
upload_url = Upload.find(user_avatar.custom_upload_id).url

```

to list all avatar URLs and deleted them from my server.  
But I guess I still need to further refresh the system/cache? How should I do this?

---

<div class="post-metadata">

**Author:** ![snakeninny](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/snakeninny/32/115522_2.png) [@snakeninny](https://meta.discourse.org/u/snakeninny)\
**Post date:** [June 5, 2021, 3:20am UTC](https://meta.discourse.org/t/how-do-i-delete-the-file-of-user-avatar/185321/15 "2021-06-05T03:20:52Z")

</div>

I got another email from my CDN provider and they have frozen my CDN account. So I have to update this thread for further help.

The new email said that the following links were illegal.

[https://cdn.iosre.com/user\_avatar/iosre.com/baal998/135/2210\_2.png](https://cdn.iosre.com/user_avatar/iosre.com/baal998/135/2210_2.png)  
[https://cdn.iosre.com/user\_avatar/iosre.com/baal998/64/2210\_2.png](https://cdn.iosre.com/user_avatar/iosre.com/baal998/64/2210_2.png)  
[https://cdn.iosre.com/user\_avatar/iosre.com/baal998/75/2210\_2.png](https://cdn.iosre.com/user_avatar/iosre.com/baal998/75/2210_2.png)  
[https://cdn.iosre.com/user\_avatar/iosre.com/baal998/75/2210\_2.png](https://cdn.iosre.com/user_avatar/iosre.com/baal998/75/2210_2.png)  
[https://cdn.iosre.com/user\_avatar/iosre.com/baal998/75/2210\_2.png](https://cdn.iosre.com/user_avatar/iosre.com/baal998/75/2210_2.png)  
[https://cdn.iosre.com/user\_avatar/iosre.com/lincwee/135/5414\_2.png](https://cdn.iosre.com/user_avatar/iosre.com/lincwee/135/5414_2.png)  
[https://cdn.iosre.com/user\_avatar/iosre.com/baal998/75/2210\_2.png](https://cdn.iosre.com/user_avatar/iosre.com/baal998/75/2210_2.png)  
[https://cdn.iosre.com/user\_avatar/iosre.com/lincwee/75/5414\_2.png](https://cdn.iosre.com/user_avatar/iosre.com/lincwee/75/5414_2.png)  
[https://cdn.iosre.com/user\_avatar/iosre.com/baal998/96/2210\_2.png](https://cdn.iosre.com/user_avatar/iosre.com/baal998/96/2210_2.png)  
[https://cdn.iosre.com/user\_avatar/iosre.com/lincwee/90/5414\_2.png](https://cdn.iosre.com/user_avatar/iosre.com/lincwee/90/5414_2.png)

As you can see these are 2 user avatars, which are memes of a former Chinese president. Again, the CDN provider asked me to delete these files on the server and refresh CDN.

I could see from the link that the users were “baal998” and “lincwee”. According to the code snippet in my last post, the uploaded avatar URL of “lincwee” was `/uploads/default/original/2X/5/55512211b1c8969c8038b79840464952cd3eb089.jpeg` while for “baal998” it was `/uploads/default/original/2X/c/cb2188eaeecc3a648f021fa00da4734bd60ca183.jpg`. I then run `find /var/discourse/shared/ -name *55512211b1c8969c8038b79840464952cd3eb089*` and deleted all files found, it worked for “lincwee”. But no luck on “baal998”, I could still access [https://iosre.com/user\_avatar/iosre.com/baal998/135/2210\_2.png](https://iosre.com/user_avatar/iosre.com/baal998/135/2210_2.png) even though there were no more files with the name `*cb2188eaeecc3a648f021fa00da4734bd60ca183*`.

So where the hell is [https://iosre.com/user\_avatar/iosre.com/baal998/135/2210\_2.png](https://iosre.com/user_avatar/iosre.com/baal998/135/2210_2.png) stored on my 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:** [June 5, 2021, 4:37am UTC](https://meta.discourse.org/t/how-do-i-delete-the-file-of-user-avatar/185321/16 "2021-06-05T04:37:32Z")

</div>

Hmm, that is a difficult situation. Maybe @falco or someone else who worked on avatars recently could advise you?

---

<div class="post-metadata">

**Author:** ![snakeninny](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/snakeninny/32/115522_2.png) [@snakeninny](https://meta.discourse.org/u/snakeninny)\
**Post date:** [June 5, 2021, 7:44am UTC](https://meta.discourse.org/t/how-do-i-delete-the-file-of-user-avatar/185321/17 "2021-06-05T07:44:10Z")

</div>

Or to make it simpler: How does Discourse parse a visit to [https://iosre.com/user\_avatar/iosre.com/baal998/135/2210\_2.png](https://iosre.com/user_avatar/iosre.com/baal998/135/2210_2.png)? If someone familiar with the source code can tell me the source file or the class/function, that would be really helpful. Thanks!

---

<div class="post-metadata">

**Author:** ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
**Post date:** [June 5, 2021, 12:18pm UTC](https://meta.discourse.org/t/how-do-i-delete-the-file-of-user-avatar/185321/18 "2021-06-05T12:18:53Z")

</div>

Look in the Uploads model, I think.

Also, start with pulling from your server, not the CDN. You want to be sure that you are solving the problem in the right place. Once you’re sure that the server isn’t sending the images you can work with the CDN.

---

<div class="post-metadata">

**Author:** ![snakeninny](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/snakeninny/32/115522_2.png) [@snakeninny](https://meta.discourse.org/u/snakeninny)\
**Post date:** [June 5, 2021, 1:03pm UTC](https://meta.discourse.org/t/how-do-i-delete-the-file-of-user-avatar/185321/19 "2021-06-05T13:03:08Z")

</div>

> [@pfaffman](#):
>
> start with pulling from your server, not the CDN

Since [https://cdn.iosre.com/user\_avatar/iosre.com/baal998/135/2210\_2.png](https://cdn.iosre.com/user_avatar/iosre.com/baal998/135/2210_2.png)is from CDN,  
[https://iosre.com/user\_avatar/iosre.com/baal998/135/2210\_2.png](https://iosre.com/user_avatar/iosre.com/baal998/135/2210_2.png)is supposed to be from my server, right?

---

<div class="post-metadata">

**Author:** ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
**Post date:** [June 5, 2021, 2:08pm UTC](https://meta.discourse.org/t/how-do-i-delete-the-file-of-user-avatar/185321/20 "2021-06-05T14:08:59Z")

</div>

Yes and you see that it is getting sent by your server. So that’s the link to be trying to fix. After you make that stop sending the images you can clear the CDN

[Next page](https://meta.discourse.org/t/how-do-i-delete-the-file-of-user-avatar/185321.md?page=2)
