{"record":{"id":"236dd9a1a44dabf0","repo":"apache/cassandra","slug":"cannot-truncate-materialized-view-directly-must-t","errorCode":null,"errorMessage":"Cannot TRUNCATE materialized view directly; must truncate base table instead","messagePattern":"Cannot TRUNCATE materialized view directly; must truncate base table instead","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/TruncateStatement.java","lineNumber":78,"sourceCode":"    public void authorize(ClientState state) throws InvalidRequestException, UnauthorizedException\n    {\n        state.ensureTablePermission(keyspace(), name(), Permission.MODIFY);\n    }\n\n    public void validate(ClientState state) throws InvalidRequestException\n    {\n        Schema.instance.validateTable(keyspace(), name());\n        Guardrails.dropTruncateTableEnabled.ensureEnabled(state);\n    }\n\n    @Override\n    public ResultMessage execute(QueryState state, QueryOptions options, Dispatcher.RequestTime requestTime) throws InvalidRequestException, TruncateException\n    {\n        try\n        {\n            TableMetadata metaData = Schema.instance.getTableMetadata(keyspace(), name());\n            if (metaData.isView())\n                throw new InvalidRequestException(\"Cannot TRUNCATE materialized view directly; must truncate base table instead\");\n\n            if (metaData.isVirtual())\n            {\n                executeForVirtualTable(metaData.id);\n            }\n            else\n            {\n                StorageProxy.truncateBlocking(keyspace(), name());\n            }\n        }\n        catch (UnavailableException | TimeoutException e)\n        {\n            throw new TruncateException(e);\n        }\n        return null;\n    }\n\n    public ResultMessage executeLocally(QueryState state, QueryOptions options)","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/TruncateStatement.java#L60-L96","documentation":"Cassandra rejects TRUNCATE issued directly against a materialized view. Views are derived, read-only structures whose data is owned by the base table, so truncating a view would corrupt the base/view consistency invariant. The check happens in TruncateStatement.execute when the resolved TableMetadata is a view.","triggerScenarios":"Executing `TRUNCATE <keyspace>.<view_name>;` (or via a driver session) where the named table is a materialized view rather than a base table.","commonSituations":"Developer lists tables in a keyspace (views appear alongside tables in schema/driver metadata), assumes the view is independently truncatable, and truncates it to 'reset' derived data instead of truncating the base table.","solutions":["Truncate the base table instead: `TRUNCATE <keyspace>.<base_table>;` — Cassandra truncates the view data along with it.","Use DESCRIBE or driver metadata (TableMetadata.isView()) to identify which name is the base table.","If the goal is only to clear view read state, drop and recreate the view after truncating the base table."],"exampleFix":"// before\nsession.execute(\"TRUNCATE myks.user_by_email;\"); // user_by_email is a materialized view\n// after\nsession.execute(\"TRUNCATE myks.users;\"); // truncate the base table","handlingStrategy":"validation","validationCode":"TableMetadata md = cluster.getMetadata().getKeyspace(ks).getTable(name);\nif (md == null || md.isView()) throw new IllegalArgumentException(name + \" is a materialized view; truncate the base table\");","typeGuard":"boolean isBaseTable(TableMetadata md) { return md != null && !md.isView(); }","tryCatchPattern":"try { session.execute(truncate); } catch (InvalidRequestException e) { if (e.getMessage().contains(\"Cannot TRUNCATE materialized view\")) truncateBaseTable(name); else throw e; }","preventionTips":["Check driver TableMetadata.isView() before issuing TRUNCATE","Name base tables explicitly in migration scripts","Treat views as read-only derived data in code reviews"],"tags":["cql","materialized-view","truncate","invalid-request"],"backgroundTag":"unsupported-operation","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}