Remove unnecessary JS in Drupal


Drupal 6

function mytheme_preprocess_page(&$vars){
    $scripts = drupal_add_js();
    //PRINT THIS ARRAY AND UNSET THE UNWANTED ARRAY// 
    unset($scripts['module']['YOUR_PATH_TO_JS']);
    $vars['scripts']= drupal_get_js('header', $scripts);
}

Drupal 7

function mymodule_js_alter(&$javascript){
  $alias = drupal_get_path_alias($_GET['q']);
  if($alias =="my-page"){
    unset($javascript['sites/all/modules/mymodule/mymodule.js']);
  }
  unset($javascript['sites/all/modules/mymodule/mymodule.js']);
}

Remove unnecessary CSS in Drupal 6 & 7


add the function to the template.php file in your theme folder

 

Drupal 6

function mytheme_preprocess_page(&$vars){
 $css = $vars['css'];
//core modules
 unset($css['all']['module']['modules/node/node.css']);
//contrib
 unset($css['all']['module']['sites/all/modules/contrib/filefield/filefield.css']);
//custom
unset($css['all']['module']['sites/all/modules/custom/mymodule/mymodule.css']);
//theme css
unset($css['all']['theme']['sites/all/themes/mytheme/mytheme.css']);

 $vars['styles'] = drupal_get_css($css);
}

 

Drupal 7

 

function mymodule_css_alter(&$css) {
  $exclude = array(
 //core
   'modules/system/maintenance.css' => FALSE,
//contrib
    'sites/all/modules/contrib/module/module.css' => FALSE,
//custom
    'sites/all/modules/custom/module/module.css' => FALSE,
//theme
    'sites/all/themes/mytheme/mytheme.css' => FALSE,
  );
  $css = array_diff_key($css, $exclude);
}