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
MG ConsultingMG Consulting 

Can you delete by Id or List of Ids?

Hi,

Can you delete by Id or List<Id>?

I thought you could due to documentation below, but when I pass delete an Id or a List<Id> it says "DML requires SObject or SObject list type: Id."


Code:
delete sObject | Record.ID

delete sObject[] | LIST:ID[] 

 
Thanks,
Mike
JeremyKraybillJeremyKraybill

DML won't accept a list of ID's directly, but you can delete objects from an ID without having to issue an additional SOQL statement like so:

 

Id del = '003S00000099999'; // whatever ID you are deleting Contact ct = new Contact(Id = del); delete ct;

 

 HTH

 

Jeremy Kraybill

Austin, TX

 

MG ConsultingMG Consulting
Ah ha, that's really good to know, thanks a lot!
disturbed118disturbed118

String pcrId= '003000789AhJKL';

 

Database.delete(pcrId);

incuGuSincuGuS

To delete one Id :

 

 

List<Id> listId = newList<Id>();

listId.add('0000000000000');
listId.add('0000000000000'); // or  a query that gets all ids you need

List<Object__c> myObjs = [SELECT Id FROM Objects__c WHERE Id IN:listId];

delete myObjs;

 thats about it, you can also delate a LIST you select with a query.

 

 

List<Whatever__c> listW = [Select Id From Whatever__c Where X__c='y'];
delete listW;

 

 

 

Ilya IvanovskiyIlya Ivanovskiy
For kind people, you can use this method. Static method, you can remove static.
Everything is simple, insert your ID and the name of the object with which you work. You're welcome =)
public static void deleteRecordById(Id removeId,String fromObject){
//        Id removeId;
//        String fromObject;
//        fromObject = 'Exchange_Rate__c';
//        removeId = 'a0B6F00001QDl6kUAD';
        String query = 'SELECT Id ';
        query += 'FROM ' + fromObject + ' ';
        query += 'WHERE Id = \'' + removeId + '\' ';
        query += 'LIMIT 1';
        List<SObject> listToRemove = Database.query(query);
//        System.debug(listToRemove);
        delete listToRemove;
    }

 
joost karsten 4joost karsten 4
There is a really simple solution for this if you want to delete a single record:
public static deleteRecordById(Id id){
        delete id.getSobjectType().newSObject(id);
}
 
Amit Kumar1Amit Kumar1

Actually, You can do both of them 

Here I am deleting multiple Accounts  or single Account by a class through a wrapper class

public class wrapperdel{
    
    @AuraEnabled
    public List<Account> accList1 = new List<Account>();
    @AuraEnabled
    public List<Account> delRec = new List<Account>();
    //Fetching total Accounts
    @AuraEnabled 
    public static List<Account> getAccounts(String query)
    {
        List<Account> accList = new List<Account>();        
        list<Account> nn = [SELECT Id,Name,Phone,Industry from Account order by LastModifiedDate desc];
        for (Account c: nn) {
            accList.add(c);
        }      
        return nn;
    }
    //Deleting the multiple selected list of id
    @AuraEnabled 
    public static List<Account> delSlctRec(List<String> slctRec)
    {
        wrapperdel alc = new wrapperdel();
        alc.delRec = [SELECT Id FROM Account WHERE Id IN: slctRec];
        Database.DeleteResult[] dlist = Database.delete(alc.delRec,false);
        for(Database.DeleteResult dr : dlist) {
            
            if (dr.isSuccess()) {
                // Operation was successful, so get the ID of the record that was processed
                System.debug('Successfully deleted account with ID: ' + dr.getId());
            }
            else {
                // Operation failed, so get all errors                
                for(Database.Error err : dr.getErrors()) {             
                    System.debug(err.getStatusCode() + ': ' + err.getMessage());
                    System.debug('Account fields that affected this error: ' + err.getFields());
                }
            }
        }
     //Optional---Finally after deletion ,quickly update the returned left records
        alc.accList1 = Database.query('SELECT Id,Name,Phone,Industry from Account');   
        return alc.accList1;
    }
}
Thanks 
Amit Kumar