Working with Delete SOQL Query: 10 Easy Methods

By: Published: September 1, 2021

Having tools that will help you to perform certain operations efficiently will go a long way in shaping how your business performs and the productivity of your day-to-day activities. It is without a doubt that applying the right steps and procedures when needed will help to reduce lost time and bring about effective execution of business plans.

Learning how to use the SOQL Delete Query is a step in the right direction as it can be very useful in helping you to overcome challenges that you might encounter along the way in trying to delete records using ordinary methods. 

This write-up will look at various ways in which you can use to study Delete SOQL Query and delete records from your data.

Table of Contents

Introduction to Salesforce

Delete SOQL Query - logo
Image Source

Salesforce is a popular cloud computing software platform that specializes in Customer Relationship Management (CRM) products. Salesforce provides a platform in which companies can connect with customers, partners, and potential customers. 

Their platform helps to ensure the growth of your company’s marketing drive, increases sales, commerce, and services as it forms a meeting point for your IT teams to work as one from anywhere across the globe, therefore, improving productivity, creating happy customers, and ensuring you sell smarter, become relevant, and grow faster.

Salesforce has a variety of modules such as Sales Cloud, Sales performance, Sales Collaboration, Marketing Cloud, etc. 

Introduction to Salesforce Object Query Language (SOQL)

Delete SOQL Query - soql logo
Image Source

Salesforce Object Query Language (SOQL) is a Salesforce query language that is primarily used to retrieve specific Salesforce records from Salesforce objects be it in standard or custom objects inside your Salesforce organization and it allows many users to share the same database.

SOQL is a language exclusively for querying the Salesforce database rather than modifying data like in traditional SQL. There are no INSERT or UPDATE statements. Modifying the data is done using Salesforce’s UI or Apex DML. Apex DML is a part of Salesforce’s proprietary programming language. In SOQL, Salesforce Objects are similar to tables in SQL.

Image Source

SOQL has similar characteristics to the SELECT statement in Structured Query Language (SQL) but is made for Salesforce data only and does not support all advanced features of the SQL SELECT command like join, wildcards in field lists, calculation expressions, etc. An example of SOQL Query is shown below.

SELECT one or more fields 
FROM an object 
WHERE filter statements and, optionally, results are ordered

Like the SELECT command in SQL, SOQL allows you to specify the source object e.g Account, a list of fields to retrieve, conditions for the selection of rows in the source object, etc. 

SOQL can be used in the following environs for the creation of query strings:

  • In the queryString parameter in the query() call.
  • In the Apex statements.
  • In the Visualforce controllers and getter methods.
  • In the Salesforce CLI or the Salesforce Extensions for Visual Studio Code.
Simplify ETL with Hevo’s No-code Data Pipeline

Hevo Data, a No-code Data Pipeline helps to Load Data from any data source such as Databases, SaaS applications, Cloud Storage, SDK,s, and Streaming Services and simplifies the ETL process. It supports 100+ data sources (including 30+ free sources etc.) and is a 3-step process by just selecting the data source, providing valid credentials, and choosing the destination. Hevo loads the data onto the desired Data Warehouse, enriches the data, and transforms it into an analysis-ready form without writing a single line of code. It helps transfer data from Salesforce to a destination of your choice for free.

Its completely automated pipeline offers data to be delivered in real-time without any loss from source to destination. Its fault-tolerant and scalable architecture ensure that the data is handled in a secure, consistent manner with zero data loss and supports different forms of data. The solutions provided are consistent and work with different Business Intelligence (BI) tools as well.

Get Started with Hevo for free

Check out why Hevo is the Best:

  • Secure: Hevo has a fault-tolerant architecture that ensures that the data is handled in a secure, consistent manner with zero data loss.
  • Schema Management: Hevo takes away the tedious task of schema management & automatically detects the schema of incoming data and maps it to the destination schema.
  • Minimal Learning: Hevo, with its simple and interactive UI, is extremely simple for new customers to work on and perform operations.
  • Hevo Is Built To Scale: As the number of sources and the volume of your data grows, Hevo scales horizontally, handling millions of records per minute with very little latency.
  • Incremental Data Load: Hevo allows the transfer of data that has been modified in real-time. This ensures efficient utilization of bandwidth on both ends.
  • Live Support: The Hevo team is available round the clock to extend exceptional support to its customers through chat, email, and support calls.
  • Live Monitoring: Hevo allows you to monitor the data flow and check where your data is at a particular point in time.
Sign up here for a 14-day Free Trial!

Working with Delete SOQL Query

There are various ways to delete records from Salesforce, in this section, a few SOQL Delete Queries and methods will be brought to your knowledge starting from the basic delete button to the more advanced options.

1) Using the Delete Button

The first type of Delete SOQL Query is deleting a single record on Salesforce is pretty straightforward, all you have to do is go to the record you want to delete and click on the standard Delete button. Upon clicking on it, the record will be deleted and sent to your recycle bin.

2) Deleting from the List View

When viewing a list of Accounts, Contacts, Leads, etc, there is a Del link displayed against each record. By clicking on the link, it will automatically delete the corresponding record and send it to the recycle bin.

3) Mass Delete Records

There is an in-built mass Delete Query in Salesforce which can be found through the Quick Find box. When deleting records through this medium, you can choose to delete records permanently or send them to the recycle bin. 

The record types you can mass-delete include cases, solutions, accounts, contacts, leads, products, and activities. You can delete up to 250 records at once. 

The mass delete can be helpful when you have multiple reports that are no longer wanted and need to be removed from a list, incorrect leads are imported and you have to delete it to start over again, delete accounts or contacts that you no longer need to do business, etc.

Before using the mass delete record option, run a Delete SOQL Query in Developer Console or a Report to view the records you will be deleting and get a record count.

For more information about mass delete records and the steps involved, visit here.

4) Using External Tools

Delete SOQL Query are also accessed by using external tools. There are various external tools available which you can use in deleting records, they include Data Loader, Jitterbit, Dataloader IO, etc. These tools give you control over how to manage your data as they can be used for deleting, inserting, and updating records. 

For further reading on how to use Dataloader, visit Salesforce official documentation here.

5) Using Apex Code

Image Source

Delete SOQL Query are also used with Apex Code. You can use Apex to create a Class or Trigger that will delete records based on your programming, for example, you can program a lead to be deleted from the records once it passes a stage by setting a trigger to delete it from lead and send it to the right category.

An Example of a delete code for a past event using Apex is shown below:

if(trigger.isUpdate) {
List<Event> lstEventsToDelete = new List<Event>();
for(Event e : trigger.new) {
Event eDel = new Event(Id=e.Id);
lstEventsToDelete.add(eDel);
}
delete lstEventsToDelete;
} 

6) Truncate Button on Custom Objects

Delete SOQL Query also provides the Truncate option. The truncate button can be used to clear records from a custom object, for example, you can delete the test records you no longer need while preserving your object configuration.

To know more about using the truncate button on custom objects, visit here.

7) Delete via URL

Apart from Delete SOQL Query Salesforce allows you to create custom buttons that can be linked to call standard functions such as delete. 

The example below shows syntax for your button and link:

https://[instance].salesforce.com/setup/own/deletedredirect.jsp?dellD=[recordID]

8) Using a Custom Visualforce Page

You can use Visualforce to also delete records on Salesforce by adding code that will be used in deleting records. The code can be written in Apex and used on Visualforce.

A Delete SOQL Query code to add a delete button on a Visualforce page is:

<apex.commandButton value=”Delete”action=”{!delete}”/>

Below is an example of a custom function written in Apex to be used on Visualforce.

public class TestClass
{
    public Sales_Rep_Goal__c lastGoal {get; private set;}
 
    public void save2()
    {
        Sales_Rep_Goal__c cd = new Sales_Rep_Goal__c();
        cd.Sales_Rep1__c = cds.Sales_Rep1__c;
        cd.Year__c = cds.Year__c;
        cd.Month__c = cds.Month__c;
        cd.Rental_Goal__c = cds.Rental_Goal__c;
        insert cd;
 
        lastGoal = cd
    }
 
    public void deleteLast()
    {
        delete lastGoal;
    }
}

Then on Visualforce

<apex:commandButton value="Delete Last" action="{!deleteLast}" rendered="{!lastGoal != null

9) Using Developer Console

With the developer console, you can enter Apex code directly into your Salesforce to enable you to delete records. To use this method, simply open the editor window and enter code by changing MyObect to the object you will like to delete from. 

For example:

delete[SELECT id FROM MyObject];

You can only delete 10,000 records using this method at a time so to avoid hitting the limit, you can change it to the statement below:

delete[SELECT id FROM MyObject LIMIT 10000];

For more information about Apex Developer Console, visit here.

10) Using an Application

There are a variety of apps found on AppExchange in Salesforce that you can use to delete records and they give you additional functionality when used to delete records. One of such apps is Mass Delete from Salesforce Labs which you can use in selecting multiple records to delete with the click of a button.

Conclusion

This article looked at various methods in which records can be deleted in Salesforce through the Delete SOQL Query in Apex, Visualforce, Developer Console, inline text in Salesforce, and using external applications.

It usually helps when you understand how to carry out the procedures laid above, however, a lot of the methods described can be truly confusing and frustrating if you are not vested with the right skills. Not to worry, there is a simpler way to manage this using Hevo Data.

Visit our Website to Explore Hevo

Hevo Data provides its users with a simpler platform for integrating data from 100+ sources such as Salesforce for Analysis. It is a No-code Data Pipeline that can help you combine data from multiple sources. You can use it to transfer data from multiple data sources into your Data Warehouse, Database, or a destination of your choice. It helps transfer data from Salesforce to a destination of your choice for free. It provides you with a consistent and reliable solution to managing data in real-time, ensuring that you always have Analysis-ready data in your desired destination.

Want to take Hevo for a spin? Sign Up for a 14-day free trial and experience the feature-rich Hevo suite first hand. You can also have a look at our unbeatable pricing that will help you choose the right plan for your business needs!

Share your experience of learning about Delete SOQL Query! Let us know in the comments section below!

Ofem Eteng
Freelance Technical Content Writer, Hevo Data

Ofem is a freelance writer specializing in data-related topics, who has expertise in translating complex concepts. With a focus on data science, analytics, and emerging technologies.

No-Code Data Pipeline for Salesforce