Search code examples
androidparse-platform

How to delete objects in parse.com after a set amount of time


Basically I want to know how I can delete an object on the server after some amount of time The main idea is to know how to delete an object after some designated time form the server automatically so objects would not persist for a long amount of time.

Thanks.


Solution

  • You must create a "job", it's a task you can schedule from your parse.com account.

    Here is an example code, it's delete all posts have more 70 days.

    Parse.Cloud.job('deleteOldPosts', function(request, status) {
    
        // All access
        Parse.Cloud.useMasterKey();
    
        var today = new Date();
        var days = 70;
        var time = (days * 24 * 3600 * 1000);
        var expirationDate = new Date(today.getTime() - (time));
    
        var query = new Parse.Query('post');
            // All posts have more than 70 days
            query.lessThan('createdAt', expirationDate);
    
            query.find().then(function (posts) {
                Parse.Object.destroyAll(posts, {
                    success: function() {
                        status.success('All posts are removed.');
                    },
                    error: function(error) {
                        status.error('Error, posts are not removed.');
                    }
                });
            }, function (error) {});
    
    });
    

    You must write this code in your main.js file, follow this steps: https://parse.com/docs/cloud_code_guide#started

    After your main.js file are upload on parse, you can schedule your job.

    Click on Jobs tabs

    It's simple, select your job name in a list and you can plan when the jobs is launch, every hours, every days..

    enter image description here