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:

https://www.postman.com/api-evangelist/workspace/discourse/documentation/35240-c739e2a5-3ddf-4ffc-90e9-7dd579e8a55d?entity=request-35240-b041dc53-b653-4c46-b067-aee2b0a5cb46

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?

2 Likes

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.

3 Likes

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.

This.

When I ran the python script below:

import requests
# Configuration
discourse_base_url = 'https://forums.mysite.me/'
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}')
        else:
            print(f'Failed to delete post {post_id}')

if __name__ == '__main__':
    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.

2 Likes

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 "https://forums.mysite.me/posts.json?username=targetusername" \
-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

2 Likes

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:
http://mysite.com/admin/users/79/delete_posts_batch ?
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 "http://forums.mysite.com/admin/users/0/delete_posts_batch"  \
-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.

2 Likes

So just take that form line out completely?

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

Delete posts using API is failing for me with 403 error with body {“errors”:[“You are not permitted to view the requested resource.”],“error_type”:“invalid_access”}

Any idea what the fix could be?