Mass Deleting Records with SQLForce

This article demonstrates how to delete 100,000s of records from Salesforce using Python and SQLForce. The example deletes Salesforce Tasks, but applies to any Salesforce table.

DANGER AHEAD: The SQLForce DELETE command can be used to rapidly destroy a Salesforce database.

Why Would I Want That?

If you subscribe to any of the mass mailing companies supporting Salesforce, you have probably accumulated many 1000s of Salesforce tasks informing you of things like:

  • An Email was sent by a robot.
  • An Email bounced.
  • An Email was rejected by a spam filter.

This information, while possibly useful when fresh, begins wasting space and money quickly as it ages.

Example

The following example deletes all Salesforce Tasks that were generated by SalesGenius (a fine company). The only tricky part of this script is the SOQL which selects tasks to delete. Enter the wrong query and you can destroy a Salesforce table or instance quickly.

import SQLForce

session = SQLForce.Session(“production” )
session.setenv(‘QUERY_ALL’, “true” ) # Force archived records to be read.

##
## SOQL that determines what records to delete. BE VERY CAREFUL HERE!
##
taskSOQL = “””SELECT id, subject FROM Task WHERE isDeleted=false AND subject LIKE ‘Genius e-mail %’ order by systemmodstamp DESC”””

nDeleted = 0
maxPerBatch = 1000 # This is a good size to prevent the Task query from timing out.
maxPerRun = 50000 # quit once this number of deletes has happened.

while True:

idsToDelete = []

soql = taskSOQL + ” LIMIT ” + str(maxPerBatch)
print(soql)
for rec in session.selectRecords(soql):

idsToDelete.append( rec.id )

if not idsToDelete:

break

nThisTime = session.delete(‘Task’, idsToDelete )

nDeleted += nThisTime
print(“Deleted ” + str(nDeleted) + ” records so far”)
if nDeleted >= maxPerRun:

break

print(“Finished”)