Is there a way to clear the PageMessages in my controller without losing my Page's viewstate?

At the moment I'm doing a ajax call from my visualforce page, which rerenders part of my page, but this could also generate new error messages, I would like to clear older error messages without having to reload my entire page and loose the viewstate.

I've tried


In my extension, without luck, and the documentation doesn't seem to be very helpful on this point either.


There are some alternative ways to display error messages, but like @Mark Pond answered you cannot clear the ApexPages.getMessages(). Also, you cannot iterate over it and replace the elements on it, or rather you can but it doesn't stick just like you observed with the clear() call.

I suspect that the List that is returned by getMessages() is defensive copy of what is actually stored.

There are alternatives, such as doing away completely with apex:pageMessages in favor of using apex:pageMessage and building up your own error message(s) to display under that tag. Obviously there are drawbacks such as maintainability with having to manually handle and report on all of the errors.

| improve this answer | |

You won't need to clear any existing messages (in fact you can't modify or clear the collection), nor will you lose your viewstate. What you are describing is exactly how the system works.

Each time the page is posted, the pagemessages collection is cleared and any new errors are added and displayed for the new request/response cycle.

Sample Working Controller:

public with sharing class PageController {

    public string myStringValue { get; set; }       
    public PageController() {}

    public PageReference action1() {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Info Message'));
        return null;
    public PageReference action2() {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Error Message'));
        return null;

and the page

<apex:page controller="PageController" >
    <apex:form >
        <apex:pageMessages id="messages" /> 
        <apex:inputText value="{!myStringValue}" />
        <apex:commandButton action="{!action1}" value="action 1" rerender="messages" />
        <apex:commandButton action="{!action2}" value="action 2" rerender="messages" />

If this doesn't describe your scenario or if you are seeing behavior that differs from this, can you provide more information and markup to support it?

| improve this answer | |
  • This is all true when you aren't using actionsupport calls (ajax) to make calls to your controller. upon receiving the response from your controller, you can perfectly add new messages (in your controller) and have them added to your page dynamically, but you can't clear / delete old values in your controller and have them dynamically removed from your page – pjcarly Jan 17 '13 at 9:49

As Peter Knolle mentioned, we can supress the error messsage that is being displayed.

I have done as follows and its working, its displaying custom message:

<apex:PageMessages rendered="{!showErrorMessages}"/>

<apex:pageMessage strength="1" severity="error" summary="Error Message2" rendered="{!NOT(showErrorMessages)}"/>



  List<ApexPages.Message> listOfMessage=ApexPages.getMessages();

   for(integer i=0;i<listOfMessage.size();i++)


   if(listOfMessage.get(i).getSummary().contains('Message 1'))



| improve this answer | |

Maybe try something like:

<apex:actionFunction action="..." rerender="pageMessages" />
<apex:pageMessages id="pageMessages" rendered="someBooleanAssignedControllerAction" />

Using the 'ActionFunction' with the partial page refresh allows you to assign a variable that will hide the <apex:pagemessage /> output. Your probably going to have to assign them using a list of string and a custom array, as others have mentioned due to limitations with them persisting in a single setate.

| improve this answer | |

After summer 18, you can now clear your messages in apex tests https://releasenotes.docs.salesforce.com/en-us/summer18/release-notes/rn_apex_clear_page_messages.htm

| improve this answer | |
  • Just curious, isn't the release only apply on the test ? – unidha Nov 2 '18 at 6:08
  • Yes it's only in apex tests. – Anand13 S May 30 '19 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.