Skip to content

Instantly share code, notes, and snippets.

@n3dst4
Last active February 19, 2016 16:29
Show Gist options
  • Save n3dst4/d6b1ee7744d706e6f80b to your computer and use it in GitHub Desktop.
Save n3dst4/d6b1ee7744d706e6f80b to your computer and use it in GitHub Desktop.
Using node-retry to work around errors in Windows when using rimraf

Deleting a tree of folders using rimraf can be pretty fraught on MS Windows; the OS itself can lock files randomly, and there may be various processes getting in the way, like virus checkers or cloud file syncing apps. rimraf itself will do an exponential-backoff-and-retry for EBUSY and ENOTEMPTY errors on Windows, but you can still see EPERM and others. If you want to do exponential-backoff-and-retry for any error, see the below recipe, using node-retry.

var rimraf = require("rimraf");
var retry = require("retry");
function safeRimraf (path, cb) {
var operation = retry.operation();
operation.attempt(function(currentAttempt) {
rimraf(path, function (err) {
if (operation.retry(err? true : null)) {
return;
}
cb(err ? operation.mainError() : null);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment