Restrict Opportunity Deletion Based on Stage

Opportunities represent one of the most important objects in Salesforce, capturing deal stages, revenue, and the history of every client interaction. But once a deal is marked as Closed Won or Closed Lost, that information becomes more than just transactional—it becomes a vital part of sales analytics, forecasting, and team performance measurement.

In this blog, we walk through a smart Apex trigger that prevents users from deleting Opportunities if the Stage is set to Closed Won or Closed Lost, unless the user holds the System Administrator profile. This automation provides an added layer of protection for your historical sales data and ensures that only authorized users can remove high-impact records.

The trigger runs in the before delete context, checking both the Opportunity stage and the profile of the user performing the deletion.

🧠 Why This Trigger Is Important


Opportunity records in Closed stages are a critical component of:

  • Sales performance reviews

  • Win/loss analysis

  • Commission tracking

  • Revenue forecasting

  • Customer lifecycle reporting

Allowing users to delete these records—accidentally or intentionally—can lead to:

  • Gaps in reporting and dashboards

  • Loss of important deal context

  • Inaccurate pipeline and historical trends

  • Reduced visibility for leadership and sales enablement teams

This trigger helps you protect valuable closed-deal data, making sure that only trusted admins can delete Opportunities once they reach a final stage.

🔍 What This Blog Covers


  • How to prevent Opportunity deletion based on stage value

  • How to restrict deletion permissions using profile-based logic

  • How to enforce business rules during the before delete trigger phase

  • Why Closed Won and Closed Lost Opportunities should be preserved

  • How to structure logic using a trigger handler class

  • Where this type of control fits into your Salesforce governance framework

This kind of validation ensures that your CRM system reflects reality—not just what’s convenient to delete.

🎯 Real-World Use Cases for This Trigger


  • Sales managers preventing reps from deleting closed deals to avoid scrutiny

  • Revenue operations maintaining closed opportunity data for reporting

  • Support and customer success teams referencing historical deals for onboarding

  • Auditors and compliance teams ensuring win/loss history is preserved

  • Salesforce admins enforcing data retention policies without relying solely on user permissions

No matter your industry or deal size, this trigger ensures that critical sales data stays intact and accessible for everyone who needs it.

👨‍💻 Developer & Admin Tips


Here’s how the logic works:

  • The trigger activates during the before delete phase of Opportunity processing

  • It checks whether the Opportunity’s Stage is either “Closed Won” or “Closed Lost”

  • It compares the current user’s profile to the System Administrator profile

  • If the user is not an admin, the deletion is blocked with a clear error message

This provides a user-friendly and enforceable validation across all interfaces—UI, API, data loader, and integrations.

The logic is structured inside a trigger handler class, which keeps your codebase modular, testable, and scalable. It’s easy to extend this logic to include:

  • Additional restricted stages

  • Role- or permission-based access rules

  • Logging or notifications on deletion attempts

Be sure to test this trigger under different conditions, including:

  • Deletion attempts by admin and non-admin users

  • Bulk deletions through list views or external tools

  • Deletion of Opportunities in other (non-closed) stages

This will help confirm that the restriction is applied consistently and that your users receive helpful, informative error messages.

🎥 Walkthrough Available – YouTube Playlist


Want to see how this works in real-time? Visit the Salesforce Makes Sense YouTube playlist, where you’ll get a full demonstration of:

  • The trigger setup and handler logic

  • How the validation responds to different user profiles

  • Live testing from both the UI and setup tools

  • Tips for adapting the rule to your org’s needs

The playlist is ideal for admins and developers who want to implement effective real-world automation without unnecessary complexity.

Solution:

trigger OpportunityTrigger on Opportunity (before delete) {
        if(Trigger.isDelete){ if(Trigger.isBefore){
                                                        OpportunityTriggerHandler.checkProfileForDeletion(Trigger.old);
                       }
          }
}
public class OpportunityTriggerHandler { public static void

             checkProfileForDeletion(List<Opportunity> oppList){

                                         Profile p = [SELECT Id FROM Profile WHERE Name = ‘System
                                Administrator’]; for(Opportunity opp:oppList){ if(opp.StageName == ‘Closed
                     Won’ || opp.StageName ==
                                          ‘Closed Lost’) if(UserInfo.getProfileId() != p.Id){ opp.addError(‘Only
                               System administrator can delete opportunity’);
                               }
                  }
     }
}

Want to Apply As Content Writer?

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart

Let's get you started!

Interested in writing Salesforce Content?

Fill in this form and we will get in touch with you :)