Delete from Source / DeleteSource.

This feature allows you to delete the records from the Source environment, which were selected from the Source environment, based on the export.json object configuration, considering the relationships between the objects.


Feature overview.

The normal behavior of the Delete operation is to delete records from the Target based ONLY on the supplied object query string or using the special deleteQuery property (see: Full export.json format). This could be a good solution, if you are able to manually create a query string, which will select for you all records you want to be deleted.

But in real situation, sometimes you can't directly select the desired records using SOQL queries, for example, it could be when you have to delete only those Contacts, which are related to some Accounts, which Accounts are releated to another Test__c object. It also may be a very complex hierarchical data model with a lot of relationships between objects that you want to delete all of them at once.

As the solution for this use case we have introduced the new deleteFromSource object's property.

In conjunction with operation="Delete", this property will enable the deletion of the source records which are result of all selections, made according to the export.json configuration, including the relationships between objects.

In general, it's working exactly like other operations (Update / Insert / Upsert), that process only the finally selected records, but in other hand the deleteFromSource working with the source data instead of the target and it makes a record deletion.

In addition, it provides the same functionality as the deleteByHierarchy feature with only one difference, that the deleteFromSource option does not require definition of both Source and Target environments in the sfdmu:run command. It intended to delete records in single salesforce org, based on the export.json object configuraton only. The deleteByHierarchy also respects the source data when deleting from the Target environment.

(Starting from the 4.2.1 ScriptObject.deleteFromSource was deprecated and replaced with the new operation=DeleteSource, however the ScriptObject.deleteFromSource=true is still supported to allow the backward export.json format compatibility)

Example.

  "objects": [
        {
            "operation": "Readonly",
            "externalId": "Name",
            "master": true,
            "query": "SELECT Id FROM Account WHERE Name LIKE 'Test%'"
        },
        {
            "operation": "Delete",
            "master": false,
            "deleteFromSource": true,
            "query": "SELECT Id, Account__c FROM TestObject__c"
        }
    ],

OR (providing the same result)

  "objects": [
        {
            "operation": "Readonly",
            "externalId": "Name",
            "master": true,
            "query": "SELECT Id FROM Account WHERE Name LIKE 'Test%'"
        },
        {
            "operation": "DeleteSource",
            "master": false,
            "query": "SELECT Id, Account__c FROM TestObject__c"
        }
    ],

The configurations above will delete only those TestObject__c records which are related to the selected Accounts.

Here you are deleting only the TestObject__c records, but of course you can also add more objects and create a complex configuration, which will make a deletion for multiple related objects at once.

!!! Please be very careful! This option deletes data from the Source organization instead of the Target !!!

When using deleteFromSource, you required to define the --sourceusername flag to salesforce organization. You can't use csvfie as the source.

Below find the sample sfdx command to run the deletion on the source org, assume you want to delete data from test@testorg.com

 sfdx sfdmu:run --sourceusername test@testorg.com

You can see that you can omit the --targetusername flag because for this export.json configuration it is useless.


See also:

Full export.json format.

Last updated on Tu May 2022