Skip to content Skip to sidebar Skip to footer

Delete All Old Objects In Parse.com

I wanted to clear all objects which a more than one day old....so I used the below given cloud code. There are many classes in my project but the below code only works for class 'M

Solution 1:

use this code its very helpful.

Parse.Cloud.job("deleteMessages", function(request, status) 

Parse.Cloud.useMasterKey();

 var ts = Math.round(newDate().getTime() / 1000);
var tsYesterday = ts - (24 * 3600);
var dateYesterday = newDate(tsYesterday*1000);

var query = newParse.Query("Your Object Class");

query.lessThan("createdAt", dateYesterday);

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');
    }
});
});

Solution 2:

"You can only perform a limited amount of asynchronous operations in Cloud Code. By calling destroy() repeatedly from a loop, you're likely to run into this restriction." - Hector From Parse

I would suggest first converting your cloud code to a "job" so your timeout is 15 minutes vs 15 seconds, then replace destroy() with:

myObject.destroy({
    success: function(myObject) {
    // The object was deleted from the Parse Cloud.
    },
    error: function(myObject, error) {
        // The delete failed.// error is a Parse.Error with an error code and description.
    }

});

And wait to continue your deletion until you have received the callback.

Post a Comment for "Delete All Old Objects In Parse.com"