Background Jobs

In this guide, we will show how to schedule a background job to an iPhone/iPad.


Step1: Create and upload the job code:

A job in Parse Cloud Code is simply a function that you can schedule to run at a certain time.

To create a job code, make a JavaScript file, then define a job using the Parse.Cloud.job function:

Parse.Cloud.job(“jobName”, function (request, status) {
    // Parameters passed with the start request
    var parameters = request.params;
    // Headers from the job request
    var headers = request.headers;
    // Logs from back4app server
    var log = request.log;
    // We need to update the status message of the job
    status.message(“The job has started.”);
    doSomething().then(function (result) {
        // This function will be executed if the doSomething function
        // finished without errors

        //Make another thing?
        //Mark the jobs as successful
        status.success(“The job has finished.”);
    }, function (error) {
        // This function will be executed if the doSomething function
        // finished with errors

        // Handle the error
        // Make another thing?

        // Mark the jobs as unsuccessful
        status.error(“There was an error.”);

Now upload the file to the Cloud Code. If you don’t know how, got to Cloud Code‘s guide.

Step 2: Schedule the Job:

Go to your app’s Features -> Background Jobs -> Server and you’ll see the screen below


Now click on “Schedule a Job”, you can view the scheduled jobs clicking on “Browser a Job”.

Complete the Job’s informations, remember that the Cloud Job’s name must be the same as the name in the function in the main.js file, then click on “Save”:schedulejob

The job will now be executed at the scheduled time.


Example: Remove unverified logins

All the used code is available in GitHub

In the following example, we show a code for a job that deletes all the users that still haven’t verified their emails after three days of the creation date.
The main.js file:

// Remove invalid logins job
var removeInvalidLogin = require('./reminval.js');

The removeInvalidLogin module make reference to the reminval.js file, that contain the code for the job.
In the reminval.js file we have:

Parse.Cloud.job("removeInvalidLogin", function (request, status) {
  var date = new Date();
  var timeNow = date.getTime();
  var intervalOfTime = 3*24*60*60*1000;  // 3 days in milliseconds
  var timeThen = timeNow - intervalOfTime;

  // Limit date
  var queryDate = new Date();

  // The query object
  var query = new Parse.Query(Parse.User);

  // Query the logins that still unverified after 3 days
  query.equalTo("emailVerified", false);
  query.lessThanOrEqualTo("createdAt", queryDate);

    success: function (results) {
      console.log("Successfully retrivied " + results.length + " invalid logins.");

      // Destroying the invalid users
      query.each(function (object, err) {
          success: function (object) {
            console.log("Successfully destroyed object " + object.objectId);
          error: function (error) {
            console.log("Error: " + error.code + " - " + error.message);
          useMasterKey: true  // VERY IMPORTANT!!
    error: function (error) {
      console.log("Error: " + error.code + " - " + error.message);

This is the code for the removeInvalidLogin job.