Gulp - Cleaning Unwanted Files


In this chapter, you will learn how to clean generated files. As we are automatically generating the files, make sure that unnecessary files should be deleted before running your build. This procedure is called cleaning. The del plugin can be used for this purpose.

Installing del Plugins

In your command line install the plugin by entering the following command.

npm install del --save-dev

Declare Dependencies and Create Tasks

In your configuration file gulpfile.js, declare the dependencies as shown in the following command.

var del = require('del');

Next, create a task as shown in the following code.

gulp.task('clean:build', function() {
   return del.sync('build');

The above task will clean entire build. The clean task clears any image catches and removes any old files present in build.

It is possible to clean only specific file or folder and leave some of them untouched as illustrated in the following code.

gulp.task('clean:build', function() {
   //return del.sync('build');
   return del([
      // instructs to clean temp folder
      // negate to instruct not to clean package.json file ]);

In the above task, only the temp folder will be cleaned leaving package.json untouched.