diff --git a/includes/token.inc b/includes/token.inc
index 3f9a5b5ce023e6cdc30d3e674a8bdc99670ec8c0..2db6c78c906e5b7d44679e7f2b3bcfea0b329d68 100644
--- a/includes/token.inc
+++ b/includes/token.inc
@@ -78,6 +78,9 @@ function token_replace($text, array $data = array(), array $options = array()) {
   $replacements = array();
   foreach (token_scan($text) as $type => $tokens) {
     $replacements += token_generate($type, $tokens, $data, $options);
+    if (!empty($options['clear'])) {
+      $replacements += array_fill_keys($tokens, '');
+    }
   }
 
   // Optionally alter the list of replacement values.
diff --git a/modules/system/system.test b/modules/system/system.test
index c5366c541f0e0cff321c9b00dd29c1ed88e02c39..049502194c4470dd545994439155293ba6ecab13 100644
--- a/modules/system/system.test
+++ b/modules/system/system.test
@@ -1449,26 +1449,30 @@ class TokenReplaceTestCase extends DrupalWebTestCase {
     $source .= '[node:author:name]';   // Node author's name
     $source .= '[node:created:since]'; // Time since the node was created
     $source .= '[current-user:name]';  // Current user's name
-    $source .= '[user:name]';          // No user passed in, should be untouched
     $source .= '[date:short]';         // Short date format of REQUEST_TIME
-    $source .= '[bogus:token]';        // Nonexistent token, should be untouched
+    $source .= '[user:name]';          // No user passed in, should be untouched
+    $source .= '[bogus:token]';        // Non-existent token
 
     $target  = check_plain($node->title);
     $target .= check_plain($account->name);
     $target .= format_interval(REQUEST_TIME - $node->created, 2, $language->language);
     $target .= check_plain($user->name);
-    $target .= '[user:name]';
     $target .= format_date(REQUEST_TIME, 'short', '', NULL, $language->language);
-    $target .= '[bogus:token]';
 
+    // Test that the clear parameter cleans out non-existent tokens.
+    $result = token_replace($source, array('node' => $node), array('language' => $language, 'clear' => TRUE));
+    $result = $this->assertFalse(strcmp($target, $result), 'Valid tokens replaced while invalid tokens cleared out.');
+
+    // Test without using the clear parameter (non-existant token untouched).
+    $target .= '[user:name]';
+    $target .= '[bogus:token]';
     $result = token_replace($source, array('node' => $node), array('language' => $language));
+    $this->assertFalse(strcmp($target, $result), 'Valid tokens replaced while invalid tokens ignored.');
 
     // Check that the results of token_generate are sanitized properly. This does NOT
     // test the cleanliness of every token -- just that the $sanitize flag is being
     // passed properly through the call stack and being handled correctly by a 'known'
     // token, [node:title].
-    $this->assertFalse(strcmp($target, $result), t('Basic placeholder tokens replaced.'));
-
     $raw_tokens = array('title' => '[node:title]');
     $generated = token_generate('node', $raw_tokens, array('node' => $node));
     $this->assertFalse(strcmp($generated['[node:title]'], check_plain($node->title)), t('Token sanitized.'));