function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Aman BhallaAman Bhalla 

Apex Code delete all Tasks based on Criteria

Hi All,

First time playing around with Developer Console. I'm trying to delete all Tasks that have been created by a user (85,000 records). I can't use Data Loader since it will max out the number of records I want to delete so instead, I was able to create a query to find all the records. I'm now trying to use APEX Code to delete all the records listed and am having some trouble.

I am trying to find all tasks that were created by a user where the subject line does not equal "Initial Lead Notification Sent" or "Secondary Lead Notification Sent". Once its executed and I have a list, I would like to delete all records. Here's what I have so far:

APEX CODE
// fetching all Tasks Records via SOQL
List<task> TaskList = new List<task>();
TaskList = [SELECT Id, subject, WhoID, ActivityDate, FROM
   task WHERE CreatedByID = '0051400000Bazbn' AND subject != 'Initial Lead Notification Sent' AND subject != 'Secondary Lead Notification Sent'];
// SOQL query for given criteria

// Delete the fetched records
delete [SELECT Id FROM TaskList LIMIT 1000];

SOQL
select id, subject, WhoID, ActivityDate FROM task where CreatedByID = '0051400000Bazbn' AND subject != 'Initial Lead Notification Sent' AND subject != 'Secondary Lead Notification Sent'

Error:
Row 3 column 10, Unexpected token "=".
Best Answer chosen by Aman Bhalla
VaasuVaasu
Why can't you use data loader, it can handle up to 5M records.
Anyways, all you have to use is one statement.
delete [SELECT Id FROM Task WHERE CreatedByID = '0051400000Bazbn' AND subject not in('Initial Lead Notification Sent','Secondary Lead Notification Sent')];
 

All Answers

VaasuVaasu
Why can't you use data loader, it can handle up to 5M records.
Anyways, all you have to use is one statement.
delete [SELECT Id FROM Task WHERE CreatedByID = '0051400000Bazbn' AND subject not in('Initial Lead Notification Sent','Secondary Lead Notification Sent')];
 
This was selected as the best answer
HARSHIL U PARIKHHARSHIL U PARIKH
You can accomplish this task using Batch Apex as well. Here is how you can proceed,

Batch Apex Code:
 
public class TaskDeleteOperation_BatchApex Implements Database.Batchable<SObject>{
    
    String id             = '0051400000Bazbn';
    String subjectLine1 = 'Initial Lead Notification Sent';
    String subjectLine2 = 'Secondary Lead Notification Sent';
    
    /*
    List<Task> tsks = [Select Id FROM Task
                                   WHERE CreatedByID =: id
                                  AND subject !=: subjectLine1
                                  AND subject !=: subjectLine2];
   */
    String Query = 'Select Id FROM Task WHERE CreatedByID =: ' + String.ValueOf(id) + ' AND ' + 'Subject !=: ' + String.ValueOf(subjectLine1) + ' AND Subject !=: ' + String.ValueOf(subjectLine2);
    
    Public Database.QueryLocator Start(Database.BatchableContext dBC){
        return Database.getQueryLocator('Query');
    }
    
    Public Void Execute(Database.BatchableContext dBC, List<Task> allComingTasks){
        If(!allComingTasks.isEmpty()){
            Delete allComingTasks;
        }
    }
    
    Public Void Finish(Database.BatchableContext dBC){
        // do nothing.
    }
}
Inorder to execute this code all you need to do is to go to your Developer Console - Debug - Open Execute Anonymous Window and type below code,
 
Id ApexBatchID = Database.executeBatch(New TaskDeleteOperation_BatchApex());
Hope this helps!

If this solves the puzzle the please mark it as Best Answer!
 
Aman BhallaAman Bhalla
Thanks Everyone. Vaasu I used your code and just added a limit. I couldn't use Dataloader since it was a trial version (max 10000 records in one month).