{"record":{"id":"19b2122058ee877a","repo":"apache/seatunnel","slug":"failed-to-truncate-collection-tablepath-getfull","errorCode":null,"errorMessage":"Failed to truncate collection: ${tablePath.getFullName()}","messagePattern":"Failed to truncate collection: (.+?)","errorType":"exception","errorClass":"CatalogException","httpStatus":null,"severity":"error","filePath":"seatunnel-connectors-v2/connector-mongodb/src/main/java/org/apache/seatunnel/connectors/seatunnel/mongodb/catalog/MongodbCatalog.java","lineNumber":185,"sourceCode":"            throws DatabaseNotExistException, CatalogException {\n        throw CommonError.unsupportedOperation(name(), \"drop database \");\n    }\n\n    @Override\n    public void truncateTable(TablePath tablePath, boolean ignoreIfNotExists)\n            throws TableNotExistException, CatalogException {\n        try {\n            if (!tableExists(tablePath)) {\n                if (ignoreIfNotExists) {\n                    return;\n                }\n                throw new TableNotExistException(name(), tablePath);\n            }\n            MongoDatabase db = mongoClient.getDatabase(tablePath.getDatabaseName());\n            MongoCollection<Document> collection = db.getCollection(tablePath.getTableName());\n            collection.deleteMany(new Document());\n        } catch (Exception e) {\n            throw new CatalogException(\n                    \"Failed to truncate collection: \" + tablePath.getFullName(), e);\n        }\n    }\n\n    @Override\n    public boolean isExistsData(TablePath tablePath) {\n        try {\n            if (!tableExists(tablePath)) {\n                return false;\n            }\n            MongoDatabase db = mongoClient.getDatabase(tablePath.getDatabaseName());\n            MongoCollection<Document> collection = db.getCollection(tablePath.getTableName());\n            return collection.estimatedDocumentCount() > 0;\n        } catch (Exception e) {\n            return false;\n        }\n    }\n","sourceCodeStart":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/apache/seatunnel/blob/cf67b549a7a6c35fa0beb12d83c62892427ea919/seatunnel-connectors-v2/connector-mongodb/src/main/java/org/apache/seatunnel/connectors/seatunnel/mongodb/catalog/MongodbCatalog.java#L167-L203","documentation":"truncateTable deletes all documents via collection.deleteMany(new Document()); any failure (existence check failure is also inside this try) is wrapped into CatalogException \"Failed to truncate collection: <fullName>\". The cause distinguishes driver errors from other failures.","triggerScenarios":"deleteMany fails due to lost connection, user lacks remove privilege on the collection, write concern timeout on a large collection, or the database/collection disappeared mid-operation.","commonSituations":"Truncating a very large collection with a short writeConcern timeout causing expiry, restricted user without delete permission, failover during the deleteMany operation.","solutions":["Inspect e.getCause(); if MongoWriteConcernException/timeout, retry with a larger wtimeout or in batches.","Grant the user remove privilege on the collection.","For very large collections, consider dropping and recreating the collection instead of deleteMany (much faster).","Verify replica set primary availability and write concern settings."],"exampleFix":"// before\ntruncateTable: collection.deleteMany(new Document());\n// after\ntry {\n    collection.deleteMany(new Document());\n} catch (MongoWriteConcernException e) {\n    db.getCollection(tableName).drop();\n    db.createCollection(tableName);\n}","handlingStrategy":"retry","validationCode":"if (!catalog.tableExists(tablePath)) {\n    throw new IllegalStateException(\"cannot truncate missing \" + tablePath);\n}","typeGuard":null,"tryCatchPattern":"try {\n    catalog.truncateTable(tablePath, true);\n} catch (CatalogException e) {\n    Throwable c = e.getCause();\n    if (c instanceof MongoWriteConcernException || c instanceof MongoTimeoutException) {\n        catalog.truncateTable(tablePath, true); // retry once\n    } else throw e;\n}","preventionTips":["Prefer drop+recreate over deleteMany for huge collections","Set generous wtimeout on write concern","Grant remove privilege on the collection","Retry truncate on transient write-concern failures"],"tags":["mongodb","catalog","truncate"],"backgroundTag":"database-write-failed","analyzedSha":"cf67b549a7a6c35fa0beb12d83c62892427ea919","analyzedAt":"2026-09-10T21:44:55.265Z","contentChangedAt":"2026-09-10T21:44:55.265Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}