Deleting a user via discourse api

User accounts can be deleted via Discourse API as well.

For example;

DELETE /admin/users/{id}.json HTTP/1.1

Content-Type: application/json
Api-Username: system
Api-Key: 1234*******

{
  "delete_posts": true,
  "block_email": false,
  "block_urls": false,
  "block_ip": false
}

* The request is simplified for readability.

As you can see the payload options allows deleting the user generated posts in the same requests, as opposed to deleting the posts prior to account deletion when accounts are deleted manually in the admin page.


However, I’m getting a 403 access denied error for this request, even though the API key has a global scope, and the user is system:

{
  "errors": ["You are not permitted to view the requested resource."],
  "error_type": "invalid_access"
}

Does it work if you change the values of the settings mentioned in the guide? The default values might block the user deletion. Not 100% sure.

I’m not sure about the specifics of the user you’re trying to delete, but the error you’re seeing likely stems from one or more of the following checks not being met.

Most likely, it’s something in the else block. And yes, I agree, the error message could definitely be less vague :grimacing:.

4 Likes

Spot on @selase! Seems like my user has more posts than User::MAX_STAFF_DELETE_POST_COUNT and also some are older than SiteSetting.delete_user_max_post_age :slight_smile:

  • delete_user_max_posts: must have been renamed to delete_user_self_max_post_count since the article posted. It is set to 1 (default)

    The maximum number of posts a user can have while allowing self-service account deletion. Set to -1 to disable self-service account deletion.

  • delete_user_max_post_age is set to 60 (default), and the user has posts over 60 days.

    Don’t allow deleting users whose first post is older than (x) days.

  • delete_all_posts_max is set to 15 (default), and my user has 12 posts

    The maximum number of posts that can be deleted at once with the Delete All Posts button. If a user has more than this many posts, the posts cannot all be deleted at once and the user can’t be deleted.

Once I eliminated the restrictions, I was able to delete the my user via the API call.

{"deleted":true}

:grin:

And perhaps API Docs could use a mention of this requirement too, maybe?

1 Like