diff --git a/lib/internal/fs/rimraf.js b/lib/internal/fs/rimraf.js index d6330fbe4399a0..6d6b19efcc98f2 100644 --- a/lib/internal/fs/rimraf.js +++ b/lib/internal/fs/rimraf.js @@ -209,12 +209,19 @@ function _rmdirSync(path, options, originalErr) { rimrafSync(join(path, child), options); }); - for (let i = 1; i <= options.maxRetries + 1; i++) { + const tries = options.maxRetries + 1; + + for (let i = 1; i <= tries; i++) { try { return rmdirSync(path, options); - } catch { - if (options.retryDelay > 0) + } 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); + } } } }