{"record":{"id":"70ce4ee9e33c07fe","repo":"prestodb/presto","slug":"schema-not-empty-70ce4e","errorCode":"SCHEMA_NOT_EMPTY","errorMessage":"Schema not empty: ","messagePattern":"Schema not empty: ","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-memory/src/main/java/com/facebook/presto/plugin/memory/MemoryMetadata.java","lineNumber":129,"sourceCode":"    {\n        if (schemas.contains(schemaName)) {\n            throw new PrestoException(ALREADY_EXISTS, format(\"Schema [%s] already exists\", schemaName));\n        }\n        schemas.add(schemaName);\n    }\n\n    @Override\n    public synchronized void dropSchema(ConnectorSession session, String schemaName)\n    {\n        if (!schemas.contains(schemaName)) {\n            throw new PrestoException(NOT_FOUND, format(\"Schema [%s] does not exist\", schemaName));\n        }\n\n        boolean tablesExist = tables.values().stream()\n                .anyMatch(table -> table.getSchemaName().equals(schemaName));\n\n        if (tablesExist) {\n            throw new PrestoException(SCHEMA_NOT_EMPTY, \"Schema not empty: \" + schemaName);\n        }\n\n        verify(schemas.remove(schemaName));\n    }\n\n    @Override\n    public synchronized ConnectorTableHandle getTableHandle(ConnectorSession session, SchemaTableName schemaTableName)\n    {\n        Long tableId = tableIds.get(schemaTableName);\n        if (tableId == null) {\n            return null;\n        }\n        return tables.get(tableId);\n    }\n\n    @Override\n    public synchronized ConnectorTableMetadata getTableMetadata(ConnectorSession session, ConnectorTableHandle tableHandle)\n    {","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-memory/src/main/java/com/facebook/presto/plugin/memory/MemoryMetadata.java#L111-L147","documentation":"MemoryMetadata.dropSchema refuses to drop a schema that still contains tables. After confirming the schema exists, it scans the tables map for any table whose schemaName matches; if found, it throws PrestoException SCHEMA_NOT_EMPTY. The schema must be emptied (all tables dropped) before it can be removed.","triggerScenarios":"DROP SCHEMA memory.<name> while tables created via CREATE TABLE memory.<name>.<t> still exist in that schema.","commonSituations":"Trying to tear down test schemas without dropping tables first; teardown scripts that skip table cleanup on failure; forgetting that memory connector has no CASCADE in this code path.","solutions":["Drop all tables first: for each table run DROP TABLE memory.<schema>.<table>","Use DROP SCHEMA ... CASCADE if the SQL layer in your version supports it for this connector","Query system metadata (memory.information_schema.tables) to enumerate tables to drop","Reorder teardown scripts to drop tables before schemas"],"exampleFix":"// before\nDROP SCHEMA memory.test_schema; -- SCHEMA_NOT_EMPTY\n// after\nDROP TABLE memory.test_schema.t1;\nDROP TABLE memory.test_schema.t2;\nDROP SCHEMA memory.test_schema;","handlingStrategy":"validation","validationCode":"// Drop all tables in the schema before dropping the schema\nList<String> tables = metadata.listTables(session, new SchemaTableName(\"memory\", schemaName))\n        .stream().map(t -> t.getTableName()).collect(toList());\nfor (String table : tables) {\n    metadata.dropTable(session, new SchemaTableName(schemaName, table));\n}\nmetadata.dropSchema(session, schemaName);","typeGuard":null,"tryCatchPattern":"try {\n    metadata.dropSchema(session, schemaName);\n} catch (PrestoException e) {\n    if (SCHEMA_NOT_EMPTY.equals(e.getErrorCode())) {\n        dropAllTablesThenRetry(schemaName);\n    } else throw e;\n}","preventionTips":["Always drop child tables before schemas in teardown scripts","Enumerate tables via information_schema/system metadata first","Fail-safe cleanup: loop until schema lists zero tables","Avoid creating tables you never clean up in shared memory catalogs"],"tags":["ddl","schema","not-empty","memory-connector"],"backgroundTag":"schema-not-empty","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}