Skip to content

Spatial Data Delete API

There are two way to delete a spatial value from an object in the database:

  1. Using Backendless Console
  2. Using API

Deleting Data with Console

To delete a spatial value in console, simply clear out the contents of a cell and press Enter. Backendless Console confirms that the object has been updated:

record-updated-point-deleted

Deleting Data with the API

Deleting a spatial value from an object using the API is technically the object update operation with the spatial value property set to null. The following algorithm can be used to perform deletion:

  1. Retrieve from the database the data object which contains the spatial property to be deleted.
  2. Set the property value to null.
  3. Save the object back in the database.

The example below demonstrates the approach:

Person class:

import com.backendless.persistence.Point;

public class Person
{
    private Point location;

    public Point getLocation()
    {
        return location;
    }

    public void setLocation( Point location )
    {
        this.location = location;
    }
}
Code to delete location in a Person object:
Backendless.Data.of( Person.class ).findFirst( new AsyncCallback<Person>()
{
    @Override
    public void handleResponse( Person firstPerson )
    {
        firstPerson.setLocation( null );
        Backendless.Data.of( Person.class ).save( firstPerson, new AsyncCallback<Person>()
        {
            @Override
            public void handleResponse( Person response )
            {
                // location has been deleted
            }

            @Override
            public void handleFault( BackendlessFault fault )
            {
                // an error has occurred
            }
        } );
    }

    @Override
    public void handleFault( BackendlessFault fault )
    {
        // an error has occurred
    }
} );

Person class:

import com.backendless.persistence.Point

class Person {
   var location: Point? = null
}
Code to delete a Point property from Person in the database:
Backendless.Data.of<Person>(Person::class.java).findFirst(object : AsyncCallback<Person> {
   override fun handleResponse(firstPerson: Person) {
       firstPerson.location = null
       Backendless.Data.of<Person>(Person::class.java).save(firstPerson, object : AsyncCallback<Person> {
           override fun handleResponse(response: Person) {
               // location has been deleted
           }

           override fun handleFault(fault: BackendlessFault) {
               // an error has occurred
           }
       })
   }

   override fun handleFault(fault: BackendlessFault) {
       // an error has occurred
   }
})

Backendless.Data.of( "Person" ).findFirst( new AsyncCallback<Map>()
{
    @Override
    public void handleResponse( Map firstPerson )
    {
        firstPerson.put( "location", null );
        Backendless.Data.of( "Person" ).save( firstPerson, new AsyncCallback<Map>()
        {
            @Override
            public void handleResponse( Map response )
            {
                // location has been deleted
            }

            @Override
            public void handleFault( BackendlessFault fault )
            {
                // an error has occurred
            }
        } );
    }

    @Override
    public void handleFault( BackendlessFault fault )
    {
        // an error has occurred
    }
} );
Backendless.Data.of("Person").findFirst(object : AsyncCallback<MutableMap<Any?, Any?>> {
   override fun handleResponse(firstPerson: MutableMap<Any?, Any?>) {
       firstPerson["location"] = null
       Backendless.Data.of("Person").save(firstPerson, object : AsyncCallback<MutableMap<Any?, Any?>> {
           override fun handleResponse(response: MutableMap<Any?, Any?>) {
               // location has been deleted
           }

           override fun handleFault(fault: BackendlessFault) {
               // an error has occurred
           }
       })
   }

   override fun handleFault(fault: BackendlessFault) {
       // an error has occurred
   }
})

Alternatively, the same result can be achieved using the bulk update API operation. With this approach you can delete a spatial property either in a single or multiple objects. This is done with a query (the where clause) used in the API request. For example, the following query uniquely identifies a single object: objectId = 'XXX-XXX-XXX'. Consider an example demonstrating the approach:

String whereClause = "email = 'bob@thebuilder.com'";
Map<String, Object> changes = new HashMap<>();
changes.put( "location", null );
Backendless.Data.of( "Person" ).update( whereClause, changes, new AsyncCallback<Integer>()
{
    @Override
    public void handleResponse( Integer objectsUpdated )
    {
        // objectsUpdated is how many objects were updated by the API call
    }

    @Override
    public void handleFault( BackendlessFault fault )
    {
        // an error has occurred
    }
} );
val whereClause = "email = 'bob@thebuilder.com'"
val changes = mutableMapOf<String, Any?>()
changes["location"] = null
Backendless.Data.of("Person").update(whereClause, changes, object : AsyncCallback {
   override fun handleResponse(objectsUpdated: Int?) {
       // objectsUpdated is how many objects were updated by the API call
   }

   override fun handleFault(fault: BackendlessFault) {
       // an error has occurred
   }
})

Codeless Reference

Consider the following records stored in the data table called geolocation:

data_spatial_update_2

Suppose you need to remove the geolocation data from the record, while leaving the value in the location column "blank". Note, that when there is no value in the column and it is "blank", the column actually contains the null value. So to delete the contents of the record, we must update the value in a specific column to the null. The example below "deletes" the current geolocation  data only for one object.

data_spatial_update_1

where:

Argument                Description
table name Name of the data table where a new record must be deleted(updated).
object An object to save in the database. Object properties must match the names of the table columns.
return result Optional parameter. When this box is checked, the operation returns the deleted(updated) object with the objectId property.

After the operation runs, the value gets deleted(replaced with the null value).

data_spatial_update_3

To delete geolocation data for multiple records, you have to use the Bulk Update Codeless block. In this case you must establish the where clause condition to allow the operation to find objects that must be delete(updated). As it was described above, the deletion result is achieved when a column is updated with the null value. The null indicates that a specific column contains no value.

The example below deletes the geolocation data for two objects in the data table. The where clause property contains the following condition:

objectId = 'E0236915-54CE-45DE-AC65-6F2093AC6ADF' OR objectId = 'F77A8ACE-EF6E-405F-9B31-E495E897D0D6'

As you can see, we have to reference two objectId values and also use the OR operator.

data_spatial_update_4

where:

Argument                Description
table name Name of the data table where records must be deleted(updated).
where clause A condition which references a column name and a specific value in the data table where an update/deletion has to be carried out. Refer to the Search With The Where Clause topic for more information.
changes This parameter expects an object that must contain the name of the column and a new value to update the existing contents with.
Return count of updated objects Optional parameter. When this box is checked, the operation returns the number of deleted(updated) objects.

The operation has successfully deleted(updated) the contents in the location column for two objects.

data_spatial_update_5