Delete all posts by a user on entire site with API?

How would I accomplish this? Can’t seem to really figure out what to do…

I’ve looked at:

but I don’t understand. The main thing I also don’t get is how to have each random ID gotten and then sent to be deleted since each ID is different…

Lola, how does one delete all Discourse posts by a user using the API?


Btw I don’t believe there is a way to bulk delete via the API. I think you need to write a script to cycle through each post to delete. Depending on how many posts, rate limits could be an issue.

Lola, how do I write a script to bulk delete Discourse posts?

BTW, Why are you using API to do this?

I think it can also be done as a rake task from command line.


Thank you, will try this out sometime today! So, can that rake stuff be done as a normal user? I would like this all to be done as a normal user who isn’t staff.


When I ran the python script below:

import requests
# Configuration
discourse_base_url = ''
api_key = 'eb4065d1dergrgh45h45h54h45reg4g70cbeb274bf7f87'
api_username = 'username'
target_username = 'targetusername'

# Headers for API requests
headers = {
    'eb4065d1dergrgh45h45h54h45reg4g70cbeb274bf7f87': api_key,
    'B': api_username

def get_user_posts(username):
    """Fetch posts by a specific user."""
    posts = []
    url = f'{discourse_base_url}/posts.json?username={username}'
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        posts_data = response.json()
        posts = posts_data.get('latest_posts', [])
    return posts

def delete_post(post_id):
    """Delete a specific post."""
    url = f'{discourse_base_url}/posts/{post_id}.json'
    response = requests.delete(url, headers=headers)
    return response.status_code == 200

def main():
    # Fetch user posts
    posts = get_user_posts(target_username)
    # Delete each post
    for post in posts:
        post_id = post.get('id')
        if delete_post(post_id):
            print(f'Successfully deleted post {post_id}')
            print(f'Failed to delete post {post_id}')

if __name__ == '__main__':

On my terminal/console, I get:

Failed to delete post 73
Failed to delete post 71
Failed to delete post 69
Failed to delete post 60
Failed to delete post 47

I tried 2 API keys I made. One with only me who can use it and one will all users. Both times, used the Global option. Same result.

I’m sorry, but this made me audibly laugh. I needed that today this morning, haha.


That’s not going to work. Even for API requests. After a certain point users can’t delete their own posts.

Usually the best option is to anonymize all posts by a user if they no longer want to be apart of the forum. This will simply change their username to something random so that their posts are no longer associated with them.

1 Like

@Lilly Saw the reply ya deleted. So, what about making it work for myself or another Admin user? Not sure if ya saw, but I edited my above post to include the result I’m getting when using that script.

I guess same can be asked to you too, @blake, haha.

This is just for testing atm.

I deleted because Blake answered already.

You can try this curl request for your post id

curl -i -sS -X DELETE "http://localhost:4200/posts/<post-id>.json"  \
-H "Content-Type: multipart/form-data"  \
-H "Api-Key: key"  \
-H "Api-Username: username"

To see if it returns a different error but likely it will just say “An Error Occurred” :frowning:

Looks like there are some site settings around users deleting posts that you can tweak. Also looks like there might be a “Delete all posts” button somewhere.

1 Like

Trying the curl command above:

curl -X GET "" \
-H "Api-Key: eb4065d56u7u6u65u54y54uy566575w4yer343434ac8a770cbeb274bf7f87" \
-H "Api-Username: AdminUsername"

So, it ran and fetched me the posts…but then I got this:

C:\Users\User>-H "Api-Key: eb4065d56u7u6u65u54y54uy566575w4yer343434ac8a770cbeb274bf7f87"
'-H' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\User>-H "Api-Username: AdminUsername"
'-H' is not recognized as an internal or external command,
operable program or batch file.

Is this normal or why is that happening?

The curl command you entered is not the same as the one from above.

Looks like you are using the Windows cmd terminal, maybe it doesn’t like the \ separating lines. You can just remove them so it is all on one line.

Tried it without the slashes already. Making it all one line fixed it, thanks!

Sorry, meant the one Lilly shared.

Looks like there is an endpoint to batch delete all posts for a user.

It’s triggered by this button on the users page from the admin dashboard


So, instead of manually having to enter a post-id (because at that point, I might as well go on the actual site and just hit the delete buttons, right?), how can that auto-get filled in for each post?

Oh, sweet! So I would just make the URL: ?
Or, what is the “79” here? a user ID I assume that you get when you go to the user’s profile?

Yes, that’s the user’s ID, and it’s a PUT request. It returns the number of deleted posts.

Oh, so I wouldn’t do?

curl -i -sS -X DELETE ""  \
-H "Content-Type: multipart/form-data"  \
-H "Api-Key: eb4065d45745678u564754y4545y545445674y34545y50cbeb274bf7f87" \
-H "Api-Username: AdminUsername"

Not DELETE but PUT request. Also, there are no form parameters here.

If it’s an admin performing deleting, there’s an admin operation to :garbage: Delete all posts for a user.

You might need to adjust other settings first to permit this (delete all posts max).

I certainly hope this isn’t the actual API key for your site. As previously noted, it’s pretty easy to figure out what your site actually is.

You should consider this key burned and rotate it.


So just take that form line out completely?

EDIT: I kept the line in and just changed “DELETE” to “PUT” and it worked.