mongodb/mongoid · error · Mongoid::Errors::DropCollectionFailure

Cannot drop collection %{collection_name}.

Error message

Cannot drop collection %{collection_name}.

What it means

Error "Cannot drop collection %{collection_name}." thrown in mongodb/mongoid.

Source

Thrown at lib/mongoid/collection_configurable.rb:34

      # @param [ true | false ] force If true, the method will drop existing
      #   collections before creating new ones. If false, the method will create
      #   only new collection (that do not exist in the database).
      #
      # @raise [ Errors::CreateCollectionFailure ] If collection creation failed.
      # @raise [ Errors::DropCollectionFailure ] If an attempt to drop collection failed.
      def create_collection(force: false)
        if collection_name.empty?
          # This is most probably an anonymous class, we ignore them.
          return
        end
        if /^system\./.match?(collection_name)
          # We do not do anything with system collections.
          return
        end

        collection.drop if force
        if coll_options = collection.database.list_collections(filter: { name: collection_name.to_s }).first
          raise Errors::DropCollectionFailure.new(collection_name) if force

          logger.debug(
            "MONGOID: Collection '#{collection_name}' already exists " +
            "in database '#{database_name}' with options '#{coll_options}'."
          )

        else
          begin
            collection.database[collection_name, storage_options.fetch(:collection_options, {})].create
          rescue Mongo::Error::OperationFailure => e
            raise Errors::CreateCollectionFailure.new(
              collection_name,
              storage_options[:collection_options],
              e
            )
          end
        end
      end

View on GitHub (pinned to 0da0e23d71)

When it happens

Trigger: Thrown at lib/mongoid/collection_configurable.rb:34 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of mongodb/mongoid@0da0e23d71 (2026-08-23). Data as JSON: /api/errors/49a732844beb1f81. Report an issue: GitHub.