How to delete all messages in the message window programmatically?

You can also do what the button in messages notebook does:

FrontEndExecute[
  { 
    FrontEndToken[FrontEnd`MessagesNotebook[], "SelectAll"], 
    FrontEndToken[FrontEnd`MessagesNotebook[], "Clear"]
  }
]

The advantage is that it may not require kernel to run at all because all symbols are known by the front end. So this will work:

Button["test", (*our code*), Evaluator -> None ]

You can obtain the NotebookObject corresponding to the messages window using MessagesNotebook, and then delete all the cells using NotebookDelete:

NotebookDelete @ Cells @ MessagesNotebook[]