# Delete PFUser via CloudCode

**URL:** <https://community.parseplatform.org/t/delete-pfuser-via-cloudcode/2710>\
**Category:** Cloud Code\
**Created:** [June 1, 2022, 8:12pm UTC](https://community.parseplatform.org/t/delete-pfuser-via-cloudcode/2710 "2022-06-01T20:12:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbrass84](https://community.parseplatform.org/user_avatar/community.parseplatform.org/tbrass84/32/1193_2.png) [@tbrass84](https://community.parseplatform.org/u/tbrass84)\
**Post date:** [June 1, 2022, 8:12pm UTC](https://community.parseplatform.org/t/delete-pfuser-via-cloudcode/2710/1 "2022-06-01T20:12:36Z")

</div>

Since Apple is requiring all apps that allow for account creation to also allow for account deletion in app, I am trying to update my app that uses Parse to follow guidelines. I have a CloudCode function set up:

```auto
    Parse.Cloud.define("deleteUserWithId", function(request, response) {
            var userId = request.params.userId;
            var query = new Parse.Query(Parse.User);
            
            query.get(userId).then(function(user) {
               
                return user.destroy(); { useMasterKey: true }
            }).then(function() {
                response.success(user);
            }, function(error) {
                response.error(error);
            });
        });

```

I then call it from the app as:

```auto
 PFUser *me = [[PFUser currentUser] fetch];
            NSString *objectID = me.username;
            [PFCloud callFunctionInBackground:@"deleteUserWithId"
                               withParameters:@{@"userId": objectID}
                                        block:^(NSString *success, NSError *error) {
                                            if (!error) {
                                                NSLog(@"Successful deletion");
                                            }
                                            else {
                                                NSLog(@"%@", error.description);
                                            }
                                        }];

```

The NSLog prints out “Successful deletion”, but in my Parse database, the user still shows up. In Cloud Code section I get this message:

`UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag`–unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)`

---

<div class="post-metadata">

**Author:** ![dblythy](https://community.parseplatform.org/user_avatar/community.parseplatform.org/dblythy/32/231_2.png) [@dblythy](https://community.parseplatform.org/u/dblythy)\
**Post date:** [June 1, 2022, 11:26pm UTC](https://community.parseplatform.org/t/delete-pfuser-via-cloudcode/2710/2 "2022-06-01T23:26:25Z")

</div>

There are a number of issues here, firstly:

> [@tbrass84](#):
>
> `return user.destroy(); { useMasterKey: true }`

Should be:

`return user.destroy(); { useMasterKey: true }`

Secondly:

> [@tbrass84](#):
>
> `response.success(user);`

The user variable isn’t available in this scope (as it’s declared in the previous `.then` callback)

Try this:

```auto
Parse.Cloud.define("deleteUserWithId", function(request, response) {
  var userId = request.params.userId;
  var query = new Parse.Query(Parse.User);
  query.get(userId).then(function(user) {
      return user.destroy({ useMasterKey: true });
  }).then(function() {
      response.success();
  }, function(error) {
      response.error(error);
  });
});

```

Although there is a key conceptual issue with this approach, as any user can delete another users’ account, which is a vulnerability. Using older Parse Server’s (which use `success` callbacks) are also very vulnerable.

A preferred approach would be:

```auto
Parse.Cloud.define("deleteUser", async function(request, response) {
  if (!request.user) {
    response.error('You are not logged in.');
    return;
  }
  try {
    await request.user.destroy({useMasterKey: true});
    response.success();
  } catch (e) {
    response.error(e);
  }
});

```

Which can be simplified even further on newer Parse Servers to:

```auto
Parse.Cloud.define("deleteUser", async ({user}) => {
  await user.destroy({useMasterKey: true});
}, {
  requireUser: true
});

```

---

<div class="post-metadata">

**Author:** ![davimacedo](https://community.parseplatform.org/user_avatar/community.parseplatform.org/davimacedo/32/316_2.png) [@davimacedo](https://community.parseplatform.org/u/davimacedo)\
**Post date:** [June 1, 2022, 11:26pm UTC](https://community.parseplatform.org/t/delete-pfuser-via-cloudcode/2710/3 "2022-06-01T23:26:41Z")

</div>

> [@tbrass84](#):
>
> `user.destroy(); { useMasterKey: true }`

It should be user.destroy({ useMasterKey: true });

But I’d highly recommend you validate which users can delete the other users. In the way this cloud code function is written, any user could use the function to delete any of the other users.

---

<div class="post-metadata">

**Author:** ![dblythy](https://community.parseplatform.org/user_avatar/community.parseplatform.org/dblythy/32/231_2.png) [@dblythy](https://community.parseplatform.org/u/dblythy)\
**Post date:** [June 1, 2022, 11:33pm UTC](https://community.parseplatform.org/t/delete-pfuser-via-cloudcode/2710/4 "2022-06-01T23:33:48Z")

</div>

Also, providing you have the delete/destory CLP set up, you should be able to call `[[PFUser currentUser] deleteInBackground...]`

---

<div class="post-metadata">

**Author:** ![tbrass84](https://community.parseplatform.org/user_avatar/community.parseplatform.org/tbrass84/32/1193_2.png) [@tbrass84](https://community.parseplatform.org/u/tbrass84)\
**Post date:** [June 2, 2022, 3:51am UTC](https://community.parseplatform.org/t/delete-pfuser-via-cloudcode/2710/5 "2022-06-02T03:51:06Z")

</div>

On the preferred approach, it is able to get the currently logged in user? Do I need to pass any parameters from the iOS app into it?

---

<div class="post-metadata">

**Author:** ![tbrass84](https://community.parseplatform.org/user_avatar/community.parseplatform.org/tbrass84/32/1193_2.png) [@tbrass84](https://community.parseplatform.org/u/tbrass84)\
**Post date:** [June 2, 2022, 3:57am UTC](https://community.parseplatform.org/t/delete-pfuser-via-cloudcode/2710/6 "2022-06-02T03:57:24Z")

</div>

I tried the first approach you mentioned, and it still did not delete

---

<div class="post-metadata">

**Author:** ![jayson](https://community.parseplatform.org/user_avatar/community.parseplatform.org/jayson/32/679_2.png) [@jayson](https://community.parseplatform.org/u/jayson)\
**Post date:** [June 3, 2022, 9:03am UTC](https://community.parseplatform.org/t/delete-pfuser-via-cloudcode/2710/7 "2022-06-03T09:03:06Z")

</div>

is it possible to have a Parse-server feature that sends out a confirmation email with a link as a secondary step to confirm account deletion (much like confirming email address) ?

It would be great for account security purposes.
