Gulp: How to delete a folder?
Asked Answered
E

4

18

I use the del package to delete a folder:

gulp.task('clean', function(){
    return del('dist/**/*', {force:true});
});

...But is there any easy way to delete the dist folder and all of its contents if it contains many subdirectories [recursively]?

Ps: I don't want to do it this way: dist/**/**/**/**/**/**/..., when there are many subdirectories.

Eb answered 21/3, 2016 at 22:13 Comment(1)
Have you tried github.com/robrich/gulp-rimraf ?Novia
W
43

your code should look like this:

gulp.task('clean', function(){
     return del('dist/**', {force:true});
});

according to the npm del docs "**" deletes all the subdirectories of dist (ps: don't delete dist folder):

"The glob pattern ** matches all children and the parent."

reference

Wreathe answered 21/3, 2016 at 22:26 Comment(1)
Thanks. Do you think is there any difference between dist/** and dist/**/*? Since ** matches '/' and its children and * matches any characters except /, I think they're same. What do you think?Eb
C
9

According to the documentation : The glob pattern ** matches all children and the parent. You have to explicitly ignore the parent directories too

gulp.task('clean', function(){
     return del(['dist/**', '!dist'], {force:true});
});

More info available here : del documentation

Changeable answered 9/6, 2017 at 13:1 Comment(0)
B
6

Imports:

const { src, dest, series, parallel } = require('gulp');
const del = require('del');

In one line:

function clean(cb) {
  del(['./dist/'], cb());
}

Or, In two lines:

function clean(cb) {
  del(['./dist/']);
  cb();
}

Finally:

exports.default = series(clean, parallel(process1, process2));
Begun answered 4/5, 2020 at 14:46 Comment(0)
G
2

If you get the error: The requested module 'del' does not provide an export named 'default', that's because del has breaking change (at least from version 7)

You should use:

import {deleteAsync} from 'del'
function clean() {
    console.log("Clean")
    return deleteAsync([
        'dest/fileToDelete*',
        'directoryToDelete'
    ])
}
Graveclothes answered 14/5, 2024 at 7:41 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.