diff --git a/xmlsitemap.drush.inc b/xmlsitemap.drush.inc
index 051a16b..4bd0964 100644
--- a/xmlsitemap.drush.inc
+++ b/xmlsitemap.drush.inc
@@ -20,6 +20,9 @@ function xmlsitemap_drush_command() {
     'description' => 'Dump and re-process all possible XML sitemap data, and then regenerate the files.',
     'callback' => 'drush_xmlsitemap_rebuild',
     'drupal dependencies' => array('xmlsitemap'),
+    'options' => array(
+      'types' => 'The types of links to rebuild, comma separated. If not provided, all link types will be used.',
+    ),
   );
   $items['xmlsitemap-index'] = array(
     'description' => 'Process un-indexed XML sitemap links.',
@@ -32,6 +35,7 @@ function xmlsitemap_drush_command() {
   $items['xmlsitemap-queue-rebuild'] = array(
     'description' => 'Dump and queues all possible XML sitemap data to be re-processed via the xmlsitemap_link_process queue. This command does not regenerate the sitemap files.',
     'options' => array(
+      'types' => 'The types of links to queue for rebuild, comma separated. If not provided, all link types will be used.',
       'limit' => 'The number of links to be processed in each queue task.',
     ),
   );
@@ -62,14 +66,21 @@ function drush_xmlsitemap_rebuild() {
   module_load_include('generate.inc', 'xmlsitemap');
 
   // Build a list of rebuildable link types.
-  $rebuild_types = xmlsitemap_get_rebuildable_link_types();
-  if (empty($rebuild_types)) {
+  $types = xmlsitemap_get_rebuildable_link_types();
+  if ($option_types = drush_get_option('types', '')) {
+    $option_types = explode(',', $option_types);
+    if ($invalid_types = array_diff($option_types, $types)) {
+      drush_set_error(dt('The following link types are invalid: @types', array('@types' => implode(', ', $invalid_types))));
+    }
+    $types = array_intersect($types, $option_types);
+  }
+  if (empty($types)) {
     return drush_set_error(dt('No link types are rebuildable.'));
   }
 
   // Run the batch process.
   timer_start('xmlsitemap_rebuild');
-  xmlsitemap_run_unprogressive_batch('xmlsitemap_rebuild_batch', $rebuild_types, TRUE);
+  xmlsitemap_run_unprogressive_batch('xmlsitemap_rebuild_batch', $types, TRUE);
 
   $vars = array(
     '@timer' => timer_read('xmlsitemap_rebuild'),
@@ -101,30 +112,37 @@ function drush_xmlsitemap_index() {
  */
 function drush_xmlsitemap_queue_rebuild() {
   module_load_include('generate.inc', 'xmlsitemap');
-  $entity_types = xmlsitemap_get_rebuildable_link_types();
-  if (empty($entity_types)) {
+
+  $types = xmlsitemap_get_rebuildable_link_types();
+  if ($option_types = drush_get_option('types', '')) {
+    $option_types = explode(',', $option_types);
+    if ($invalid_types = array_diff($option_types, $types)) {
+      drush_set_error(dt('The following link types are invalid: @types', array('@types' => implode(', ', $invalid_types))));
+    }
+    $types = array_intersect($types, $option_types);
+  }
+  if (empty($types)) {
     return drush_set_error(dt('No link types are rebuildable.'));
   }
 
-  $context = array();
-  xmlsitemap_rebuild_batch_clear($entity_types, TRUE, $context);
+  xmlsitemap_rebuild_clear($types, TRUE);
 
   $link_count = 0;
   $chunk_count = 0;
   $chunk_size = (int) drush_get_option('limit', variable_get('xmlsitemap_batch_limit', 100));
 
   // @todo Figure out how to re-use this code with xmlsitemap_rebuild_batch_fetch()
-  foreach ($entity_types as $entity_type) {
-    $info = xmlsitemap_get_link_info($entity_type);
+  foreach ($types as $type) {
+    $info = xmlsitemap_get_link_info($type);
     $query = new EntityFieldQuery();
-    $query->entityCondition('entity_type', $entity_type);
+    $query->entityCondition('entity_type', $type);
     $query->entityCondition('entity_id', 0, '>');
     $query->addTag('xmlsitemap_link_bundle_access');
     $query->addTag('xmlsitemap_rebuild');
-    $query->addMetaData('entity', $entity_type);
+    $query->addMetaData('entity', $type);
     $query->addMetaData('entity_info', $info);
-    if ($types = xmlsitemap_get_link_type_enabled_bundles($entity_type)) {
-      $query->entityCondition('bundle', $types, 'IN');
+    if ($bundles = xmlsitemap_get_link_type_enabled_bundles($type)) {
+      $query->entityCondition('bundle', $bundles, 'IN');
     }
     else {
       // If no enabled bundle types, skip everything else.
@@ -132,13 +150,13 @@ function drush_xmlsitemap_queue_rebuild() {
     }
 
     $results = $query->execute();
-    if (!empty($results[$entity_type])) {
-      $ids = array_keys($results[$entity_type]);
+    if (!empty($results[$type])) {
+      $ids = array_keys($results[$type]);
       $link_count += count($ids);
       $chunks = array_chunk($ids, $chunk_size);
       $chunk_count += count($chunks);
       foreach ($chunks as $chunk) {
-        xmlsitemap_link_enqueue($entity_type, $chunk);
+        xmlsitemap_link_enqueue($type, $chunk);
       }
     }
   }
@@ -149,4 +167,5 @@ function drush_xmlsitemap_queue_rebuild() {
   else {
     drush_log(dt('No links to queue for rebuild processing.'), 'ok');
   }
+  variable_set('xmlsitemap_rebuild_needed', FALSE);
 }
diff --git a/xmlsitemap.generate.inc b/xmlsitemap.generate.inc
index b7cf4ed..57c3bc1 100644
--- a/xmlsitemap.generate.inc
+++ b/xmlsitemap.generate.inc
@@ -424,22 +424,8 @@ function xmlsitemap_batch_variable_set(array $variables) {
  */
 function xmlsitemap_rebuild_batch_clear(array $entities, $save_custom, &$context) {
   if (!empty($entities)) {
-    // Let other modules respond to the rebuild clearing.
-    module_invoke_all('xmlsitemap_rebuild_clear', $entities, $save_custom);
-
-    $query = db_delete('xmlsitemap');
-    $query->condition('type', $entities);
-
-    // If we want to save the custom data, make sure to exclude any links
-    // that are not using default inclusion or priority.
-    if ($save_custom) {
-      $query->condition('status_override', 0);
-      $query->condition('priority_override', 0);
-    }
-
-    $query->execute();
+    xmlsitemap_rebuild_clear($entities, $save_custom);
   }
-
   $context['message'] = t('Purging links.');
 }
 
@@ -534,3 +520,32 @@ function xmlsitemap_get_rebuildable_link_types() {
 
   return $rebuild_types;
 }
+
+/**
+ * Clear all sitemap links for given entity types.
+ *
+ * @param array $types
+ *   An array of link types.
+ * @param bool $save_custom
+ *   A boolean if links with status or priority overridden should not be
+ *   removed (and hence overridden values not lost).
+ *
+ * @return int
+ *   The number of deleted links.
+ */
+function xmlsitemap_rebuild_clear(array $types, $save_custom) {
+  // Let other modules respond to the rebuild clearing.
+  module_invoke_all('xmlsitemap_rebuild_clear', $types, $save_custom);
+
+  $query = db_delete('xmlsitemap');
+  $query->condition('type', $types);
+
+  // If we want to save the custom data, make sure to exclude any links
+  // that are not using default inclusion or priority.
+  if ($save_custom) {
+    $query->condition('status_override', 0);
+    $query->condition('priority_override', 0);
+  }
+
+  return $query->execute();
+}
diff --git a/xmlsitemap.xmlsitemap.inc b/xmlsitemap.xmlsitemap.inc
index 3e23622..6f3b783 100644
--- a/xmlsitemap.xmlsitemap.inc
+++ b/xmlsitemap.xmlsitemap.inc
@@ -117,14 +117,16 @@ class XMLSitemapWriter extends XMLWriter {
    *   An array of the elements properties and values.
    */
   public function writeSitemapElement($name, array &$element) {
-    $this->writeElement($name, $element);
-    $this->writeRaw(PHP_EOL);
+    if (!empty($element)) {
+      $this->writeElement($name, $element);
+      $this->writeRaw(PHP_EOL);
 
-    // After a certain number of elements have been added, flush the buffer
-    // to the output file.
-    $this->sitemapElementCount++;
-    if (($this->sitemapElementCount % $this->linkCountFlush) == 0) {
-      $this->flush();
+      // After a certain number of elements have been added, flush the buffer
+      // to the output file.
+      $this->sitemapElementCount++;
+      if (($this->sitemapElementCount % $this->linkCountFlush) == 0) {
+        $this->flush();
+      }
     }
   }
 
