Errno::ENOTEMPTY
RubyWARNINGCriticalFilesystem

Directory not empty

Quick Answer

Use FileUtils.rm_rf to recursively remove a directory and all its contents.

What this means

Raised when Dir.rmdir or an equivalent call attempts to remove a directory that still contains files or subdirectories. Maps to POSIX errno 39.

Why it happens
  1. 1Calling Dir.rmdir on a directory that contains files
  2. 2Calling File.unlink on a directory (wrong call) that is not empty

Fix

Recursively remove with FileUtils.rm_rf

Recursively remove with FileUtils.rm_rf
require 'fileutils'
FileUtils.rm_rf('old_build/')   # removes directory and all contents

Why this works

rm_rf recursively removes the directory tree, equivalent to rm -rf.

Code examples
Reproducing the errorruby
Dir.rmdir('/tmp/nonempty')
# Errno::ENOTEMPTY: Directory not empty - /tmp/nonempty
Safe recursive removalruby
FileUtils.rm_rf('tmp/cache')
Rescue and retryruby
begin
  Dir.rmdir(dir)
rescue Errno::ENOTEMPTY
  FileUtils.rm_rf(dir)
end
Sources
Official documentation ↗

Ruby Core Documentation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Ruby errors