4

Is there an easy way to delete all objects within a particular class (i.e. messages) which satisfy a particular condition, such as "UserID" = user, so that all of the rows within my message class associated with a particular user will be deleted?

2
  • You will need to elaborate further, I have no idea what you're talking about. Commented Feb 13, 2014 at 20:01
  • What particular class are you using? Sample code will help us help you. Commented Feb 13, 2014 at 20:01

2 Answers 2

13

Try this,

 PFQuery *query = [PFQuery queryWithClassName:@"messages"];
 [query whereKey:@"UserID" equalTo:@"user"];
 [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
 if (!error) {
     // The find succeeded.
     NSLog(@"Successfully retrieved %d scores.", objects.count);
     // Do something with the found objects
     for (PFObject *object in objects) {
         [object deleteInBackground];
     }
 } else {
     // Log details of the failure
     NSLog(@"Error: %@ %@", error, [error userInfo]);
 }
}];

Update

replace

for (PFObject *object in objects) {
    [object deleteInBackground];
}

with

[PFObject deleteAllInBackground:objects];

thanks mikewoz for the update.

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

4 Comments

Is there a more effective way to do this so that you don't have to launch a query to find the objects?
this seems query intensive
This is a very sought-after functionality among the community, but Parse has yet to implement it. The solution @Akhilrajtr offers is quite bandwidth-intensive if there are many objects. To alleviate that, you could create a cloud code function to handle it all in the backend.
Instead of using [object deleteInBackground] in a for loop, you should use [PFObject deleteAllInBackground:objects] to do it all in one request. More info on this call here: blog.parse.com/2013/07/22/…
0

This helped me finally figure out how to delete all related pointer objects of a deleted object while using Parse and the local store.

Hope this saves someone a few hours.

Parse.Cloud.afterDelete("Student", function(request) {

  // after delete a student find the associated sessions and remove
  var query = new Parse.Query("Session");

    var userPointer = {
      __type: 'Pointer',
      className: 'Student',
      objectId: request.object.id
    }

  query.equalTo("studentOwner", userPointer);

  query.find().then(function(studentSessions) {
    return Parse.Object.destroyAll(studentSessions);
  }).then(function(success) {
    // The related sessions were deleted
  }, function(error) {
    console.error("Error deleting related sessions " + error.code + ": " + error.message);
  });
});

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.