Intuition

One of our clients requests that the webform send emails with the files uploaded through the form (which is feasible via Webform > Email / Handlers), but they do not want the Drupal system to store any submission data (form data) or uploaded files (including in Drupal’s private file system). In other words, they require the webform submission to self-destruct immediately after the email is sent.

While turning off the submission data is simple, by going to “Webform > Settings > General” and check the “Disable saving of submissions” toggle (see screenshot: link), there is no built-in feature to delete the private files upon email submission.

I thought about deleting it via a CRON job (e.g. rm -rf site/default/files/private/webform/webform_machine_name/* -> cron_bash.sh) , but then if a user would to submit an email just the second before the CRON job ran, his files might have a chance to be deleted before the webform triggers the email send (the files may be lost during this process). Moreover, this does not guarantee the timeliness of the deletion, the CRON job can only run as frequent as 1min/execution, and that’s just not good enough !

(Scribble) My Process of Exploration

  1. Randomly adding XDebug breakpoint / logging to find an entry point (since I can find nothing on the official document about the presence of any hook that is capable of overriding the default webform email behaviour):

    1-add-log-in-an-effort-to-find-handler-function

  2. Found this sendMessage() function in file: <root>/modules/contrib/webform/src/Plugin/WebformHandler/EmailWebformHandler.php being trigged upon webform submission that is responsible for sending the email:

    2-showcase-xdebug-break-on-sendMessage

  3. Making some inline modification to attempt to modify the default behaviour in Drupal\webform\Plugin\WebformHandler\EmailWebformHandler (to delete all files in the corresponding private directory upon successful email submission):

    3-adding-additional-logic-inplace-to-test

  4. Turning the changes into a installable custom module that extends the Drupal\webform\Plugin\WebformHandler\EmailWebformHandler to modify its sendMessage behaviour:

2025-11-20T130030

Solution

Custom module webform_email_cleanup that purge the private system of webform upon successful email sending: module / custom / webform_email_cleanup.zip

*to container additional webform, please modify the sendMessage() function to include your webform machine name at line-33 :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class EmailWebformHandler extends OriginalEmailWebformHandler {
  public function sendMessage(WebformSubmissionInterface $webform_submission, array $message) {
  
    // Call parent sendMessage to send the email.
    $result_send = parent::sendMessage($webform_submission, $message);

    // Clean up uploaded email attachment files after sending for certain webforms.
    if($result_send){
-	    if ($this->webform->id() === 'webform_machine_name'){
+       if ($this->webform->id() === 'example_webform_1' || $this->webform->id() === 'example_webform_2') {
            $this->cleanupAttachmentFiles($webform_submission);
        }
    }

    // Return only the send status (alike the original function).
    return $result_send;
  }

Final outcome showcase:

2025-11-20T133506