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…
A user wants to delete all posts by a specific user on a Discourse site using the API. Lilly provides a step-by-step guide on how to accomplish this, including fetching user posts and deleting each post individually using the DELETE /posts/{id}.json endpoint. However, Lilly notes that rate limits could be an issue and suggests writing a script to cycle through each post.
45thj5ej tries the provided Python script but encounters issues with deleting posts. blake explains that users can’t delete their own posts after a certain point and suggests anonymizing posts instead. 45thj5ej wants to perform the task as a normal user or admin user.
blake provides a curl command to delete a post, which 45thj5ej tries but encounters issues with the command format. blake explains that the issue is due to the Windows cmd terminal not liking the line breaks.
blake discovers an endpoint to batch delete all posts for a user, triggered by a button on the user’s page in the admin dashboard. Arkshine explains that the endpoint is a PUT request and requires the user’s ID.
45thj5ej tries the PUT request but encounters issues with the request format. Arkshine points out that the request should not include form parameters. 45thj5ej eventually gets the request working by changing the request method from DELETE to PUT.
supermathie notes that if an admin is performing the deletion, there’s an admin operation to delete all posts for a user, but other settings may need to be adjusted first. They also warn 45thj5ej that their API key may have been compromised.
discord_user1031 encounters a 403 error when trying to delete posts using the API and asks for help.
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.
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.
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.
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” ![]()
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.
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.
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
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.
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?