[SalesForce] Too many SOQL rows returned (limit exception) – 50001

I am getting

Too many Soql rows returned:50001 (limit exception)

on below line of code

list<ContentVersion> cvlist= [Select c.Id, c.ContentDocumentId From ContentVersion c 
                              where c.Syn_Documents__c!=''];

global class DeleteUploadDocOnDeleteOfContentBatchapex implements Database.Batchable<sObject>,Database.Stateful{

global DeleteUploadDocOnDeleteOfContentBatchapex () {

}

global Database.QueryLocator<SObject> start(Database.BatchableContext BC){

   return Database.getQueryLocator('Select u.Id, u.Content_Id__c, u.ContentDocumentId__c From Project_Documents__c u where u.ContentDocumentId__c!=\'\'');

}
 global void execute(Database.BatchableContext BC, List<sObject> scope){

  list<Project_Documents__c> doclist = (list<Project_Documents__c>)scope;

  map<String,Project_Documents__c> umap = new map<String,Project_Documents__c>();

  for(Project_Documents__c u : doclist){
    umap.put(u.Content_Id__c,u);    
 }


 **list<ContentVersion> cvlist= [Select c.Id, c.ContentDocumentId From ContentVersion c where c.Syn_Documents__c!=''];**


 list<Project_Documents__c> docList2 = new list<Project_Documents__c>();

 for(ContentVersion cv: cvlist){
  if(umap.containsKey(cv.Id))
    docList2.add(umap.get(cv.Id));       
 } 


 map<String,Project_Documents__c> umap2 = new map<String,Project_Documents__c>();

 for(Project_Documents__c u:doclist2){
    umap2.put(u.Content_Id__c,u);   
 }


list<Project_Documents__c> docList2Delete = new list<Project_Documents__c>();

for(Project_Documents__c u:doclist){ 
   if(!umap2.containsKey(u.Content_Id__c))
     docList2Delete.add(u);
}


if(docList2Delete.size()>0)
  delete docList2Delete;

}

 global void finish(Database.BatchableContext BC){


   }

}

Best Answer

The 50,000 mentioned means you must be encountering a "Too Many Rows Returned" Limit Exception and not a "Too Many SOQL Queries".

Your query is too open ended. Try adding a more restrictive where clause.

[Select c.Id, c.ContentDocumentId 
   From ContentVersion c where c.Syn_Documents__c!='' AND Id in :umap.keySet()];