diff --git a/lib/internal/fs/rimraf.js b/lib/internal/fs/rimraf.js index 38bbb66a47df9b..3402af60a4022f 100644 --- a/lib/internal/fs/rimraf.js +++ b/lib/internal/fs/rimraf.js @@ -184,7 +184,7 @@ function rimrafSync(path, options) { if (stats !== undefined && stats.isDirectory()) _rmdirSync(path, options, null); else - unlinkSync(path); + _unlinkSync(path, options); } catch (err) { if (err.code === 'ENOENT') return; @@ -198,6 +198,25 @@ function rimrafSync(path, options) { } +function _unlinkSync(path, options) { + const tries = options.maxRetries + 1; + + for (let i = 1; i <= tries; i++) { + try { + return unlinkSync(path); + } catch (err) { + // Only sleep if this is not the last try, and the delay is greater + // than zero, and an error was encountered that warrants a retry. + if (retryErrorCodes.has(err.code) && + i < tries && + options.retryDelay > 0) { + sleep(i * options.retryDelay); + } + } + } +} + + function _rmdirSync(path, options, originalErr) { try { rmdirSync(path); @@ -264,7 +283,7 @@ function fixWinEPERMSync(path, options, originalErr) { if (stats.isDirectory()) _rmdirSync(path, options, originalErr); else - unlinkSync(path); + _unlinkSync(path, options); }