carrierwaveuploader/carrierwave · error · NotImplementedError

Need to implement #delete_dir! if you want to use #{self.cla

Error message

Need to implement #delete_dir! if you want to use #{self.class.name} as a cache storage.

What it means

NotImplementedError raised by the delete_dir! stub on CarrierWave::Storage::Abstract. After a cached file is cleaned up, CarrierWave asks the cache storage to remove the (now empty) per-file cache directory via delete_dir!; engines that never implemented the cache interface fail here, typically right after a successful or failed upload cycle.

Source

Thrown at lib/carrierwave/storage/abstract.rb:35

        uploader.deduplicated_filename
      end

      def store!(file)
      end

      def retrieve!(identifier)
      end

      def cache!(new_file)
        raise NotImplementedError, "Need to implement #cache! if you want to use #{self.class.name} as a cache storage."
      end

      def retrieve_from_cache!(identifier)
        raise NotImplementedError, "Need to implement #retrieve_from_cache! if you want to use #{self.class.name} as a cache storage."
      end

      def delete_dir!(path)
        raise NotImplementedError, "Need to implement #delete_dir! if you want to use #{self.class.name} as a cache storage."
      end

      def clean_cache!(seconds)
        raise NotImplementedError, "Need to implement #clean_cache! if you want to use #{self.class.name} as a cache storage."
      end
    end # Abstract
  end # Storage
end # CarrierWave

View on GitHub (pinned to b5f0abe10e)

Solutions

  1. Set config.cache_storage = :file so the file storage handles cache dir lifecycle
  2. Implement #delete_dir!(path) (usually a no-op or recursive rmdir) in your storage subclass
  3. Upgrade CarrierWave when relying on bundled engines like fog for caching

Example fix

# before
class Storage::MyDb < CarrierWave::Storage::Abstract
  # no cache methods -> delete_dir! NotImplementedError during cleanup
end

# after
class Storage::MyDb < CarrierWave::Storage::Abstract
  def delete_dir!(path); FileUtils.rm_rf(File.dirname(path)); end
end
Defensive patterns

Strategy: validation

Validate before calling

cache_engine = (MyUploader.cache_storage || MyUploader.storage)
raise "cache storage must implement #delete_dir!" unless cache_engine.instance_methods.include?(:delete_dir!)

Type guard

def cache_capable_storage?(klass)
  %i[cache! retrieve_from_cache! delete_dir! clean_cache!].all? { |m| klass.instance_methods.include?(m) }
end

Prevention

When it happens

Trigger: Using a store-only storage engine as cache storage (cache_storage unset and storage custom, or cache_storage explicitly set to an engine missing cache methods) so CarrierWave::Uploader::Cache's cleanup path calls cache_storage.delete_dir!(path) on the Abstract stub.

Common situations: Same family as the other cache stubs: half-finished custom storages discovered only when cache cleanup runs (often after the first upload in a new environment), fog-as-cache on old versions, or incomplete test doubles.

Related errors


AI-assisted analysis of carrierwaveuploader/carrierwave@b5f0abe10e (2026-08-21). Data as JSON: /api/errors/68cfeb4e75f65808. Report an issue: GitHub.