{"record":{"id":"494a0f6801089d7f","repo":"apache/cassandra","slug":"cannot-use-drop-table-on-a-materialized-view-plea","errorCode":null,"errorMessage":"Cannot use DROP TABLE on a materialized view. Please use DROP MATERIALIZED VIEW instead.","messagePattern":"Cannot use DROP TABLE on a materialized view\\. Please use DROP MATERIALIZED VIEW instead\\.","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/DropTableStatement.java","lineNumber":103,"sourceCode":"        Guardrails.dropTruncateTableEnabled.ensureEnabled(state);\n\n        Keyspaces schema = metadata.schema.getKeyspaces();\n        KeyspaceMetadata keyspace = schema.getNullable(keyspaceName);\n\n        TableMetadata table = null == keyspace\n                            ? null\n                            : keyspace.getTableOrViewNullable(tableName);\n\n        if (null == table)\n        {\n            if (ifExists)\n                return schema;\n\n            throw ire(\"Table '%s.%s' doesn't exist\", keyspaceName, tableName);\n        }\n\n        if (table.isView())\n            throw ire(\"Cannot use DROP TABLE on a materialized view. Please use DROP MATERIALIZED VIEW instead.\");\n\n        if (table.requiresAccordSupport() && table.params.pendingDrop)\n            throw ire(\"Table '%s.%s' is already being dropped\", keyspaceName, tableName);\n\n        Iterable<ViewMetadata> views = keyspace.views.forTable(table.id);\n        if (!isEmpty(views))\n        {\n            throw ire(\"Cannot drop a table when materialized views still depend on it (%s)\",\n                      keyspaceName,\n                      join(\", \", transform(views, ViewMetadata::name)));\n        }\n\n        return schema.withAddedOrUpdated(keyspace.withSwapped(keyspace.tables.without(table)));\n    }\n\n    SchemaChange schemaChangeEvent(KeyspacesDiff diff)\n    {\n        return new SchemaChange(Change.DROPPED, Target.TABLE, keyspaceName, tableName);","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/DropTableStatement.java#L85-L121","documentation":"Kind guard in DropTableStatement.apply(): the object resolved for keyspaceName.tableName is a materialized view rather than a table (keyspace.getTableOrViewNullable returned a ViewMetadata). Views and tables share a namespace but have different drop procedures, so DROP TABLE is rejected with InvalidRequestException directing the user to DROP MATERIALIZED VIEW.","triggerScenarios":"DROP TABLE ks.view_name where the resolved metadata is a ViewMetadata (table.isView() == true) in DropTableStatement.apply().","commonSituations":"Iterating all keyspace object names and dropping them generically with DROP TABLE; scripts that read names from system_schema where views also appear; confusing a view with its base table.","solutions":["Re-issue the statement as DROP MATERIALIZED VIEW [IF EXISTS] keyspace.view_name; verify with DESCRIBE whether the target object is a table or a view; add IF EXISTS if the drop should silently skip missing objects."],"exampleFix":"// before\nDROP TABLE ks.user_by_email;\n// after\nDROP MATERIALIZED VIEW ks.user_by_email;","handlingStrategy":"validation","validationCode":"boolean isView = schema.getKeyspace(ks).views.get(name).isPresent();\nif (isView) { use dropMaterializedView(ks, name); } else { use dropTable(ks, name); }","typeGuard":"boolean isMaterializedView(String ks, String name) { return schema.getKeyspace(ks).views.get(name).isPresent(); }","tryCatchPattern":"catch (InvalidRequestException e) { if (e.getMessage().contains(\"DROP MATERIALIZED VIEW\")) { /* retry with DROP MATERIALIZED VIEW */ } else throw e; }","preventionTips":["Classify objects as table vs view via metadata before dropping","Never bulk-drop all names in a keyspace without filtering views","Remember materialized views cannot be dropped with DROP TABLE"],"tags":["cql","schema","materialized-view"],"backgroundTag":"unsupported-operation","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}