0

I am trying to delete all my Parse.com users that have a certain value. So if schoolName == NHS they should be deleted. I have code that might work but I am not confident in it and I would appreciate if someone could help confirm that this should work, and if this is the best way to do it.

Parse.Cloud.job("deleteUsers", function(request, status) 
Parse.Cloud.useMasterKey();

var query = new Parse.Query("_User");
query.lessThan("schoolName", "NHS");
query.find({
    success: function(result) {
        for(var i=0; i<result.length; i++) {
            result[i].destroy({
                success: function(object) {
                    status.success("Delete job completed");
                    alert('Delete Successful');
                },
                error: function(object, error) {
                    status.error("Delete error :" + error);
                    alert('Delete failed');
                }
            });
        }
        status.success("Delete job completed");
    },
    error: function(error) {
        status.error("Error in delete query error: " + error);
        alert('Error in delete query');
    }
});

Thanks!

2 Answers 2

1

If you are afraid of making mistake, the following might help you and run your code. Using exportation to export all the data and you will receive it from your Parse.com email. Then, import all the exported data into the new app for testing. Then, it will not change your original app in Parse.com.

Sign up to request clarification or add additional context in comments.

1 Comment

This is the best way to check if cloud code does what you expect it to do!
0

using for to loop in your results won't work, try using promise instead:

Parse.Cloud.job("deleteUsers", function(request, status) {
    Parse.Cloud.useMasterKey();
    var query = new Parse.Query("_User");
    query.lessThan("schoolName", "NHS");
    return query.find().then(function(users) {
        return Parse.Promise.when(results.map(function(user) {
            user.destroy();
        }));
    }).then(function() {
        status.success("Delete job completed");
    }, function(error) {
        status.error(error);
    });
});

to avoid delete data, just remove user.destroy() and replace it with console.log(user.id) to check with each objectId.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.