Deleting Records with SQLForce
SQLForce has two techniques for deleting records in Salesforce:
- A standard ANSI DELETE statement.
- A delete() method for when Salesforce record Ids are known.
With SQLForce you do not need to worry about the Salesforce “200 records per delete call” restriction. SQLForce will batch records for you automatically.
DANGER AHEAD: The SQLForce DELETE command can be used to rapidly destroy a Salesforce database.
ANSI DELETE Statement
SQLForce supports a DELETE statement of the form:
- DELETE FROM tableName WHERE <condition>
- DELETE FROM Contact WHERE lastName=’Smith’ and firstName=’Gregory’ and account.name=’Capstorm’
- DELETE FROM Account WHERE name LIKE ‘Capstorm%’
The SQLForce method Session.runCommands() is used to execute these statements, for example:
session = SQLForce.Session(‘MyProfile’)
session.runCommands(“DELETE FROM Account WHERE name LIKE ‘Capstorm%'”)
nDeleted = session.getenv(‘ROW_COUNT’)
The session.getenv() call retrieves a SQLForce variable that holds the count of rows affected by the most recent INSERT/UPDATE/DELETE statement.
The Session.delete() method is usually a safer approach for deleting records because it requires the Salesforce record Ids to be known, making it more difficult to destroy a table with a typo in a WHERE clause.
- session.delete(‘TableName’, <list of Salesforce ids to delete>)
- session.delete(‘Account’, [‘001A0000014fia3’, ‘001A0000013RRcl’])
In this example:
- The first argument, ‘Account’, is the name of the table from which to delete records.
- The second argument is a list of record Ids to delete.
The session.delete() method returns a count of the number of records deleted.