apache/seatunnel · error · CatalogException

Failed to truncate collection

Error message

Failed to truncate collection: ${tablePath.getFullName()}

What it means

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.

Solutions

  1. Inspect e.getCause(); if MongoWriteConcernException/timeout, retry with a larger wtimeout or in batches.
  2. Grant the user remove privilege on the collection.
  3. For very large collections, consider dropping and recreating the collection instead of deleteMany (much faster).
  4. Verify replica set primary availability and write concern settings.

Example fix

// before
truncateTable: collection.deleteMany(new Document());
// after
try {
    collection.deleteMany(new Document());
} catch (MongoWriteConcernException e) {
    db.getCollection(tableName).drop();
    db.createCollection(tableName);
}
Defensive patterns

Strategy: retry

Validate before calling

if (!catalog.tableExists(tablePath)) {
    throw new IllegalStateException("cannot truncate missing " + tablePath);
}

Try / catch

try {
    catalog.truncateTable(tablePath, true);
} catch (CatalogException e) {
    Throwable c = e.getCause();
    if (c instanceof MongoWriteConcernException || c instanceof MongoTimeoutException) {
        catalog.truncateTable(tablePath, true); // retry once
    } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: Truncating a very large collection with a short writeConcern timeout causing expiry, restricted user without delete permission, failover during the deleteMany operation.

Related errors


AI-assisted analysis of apache/seatunnel@cf67b549a7 (2026-09-10). Data as JSON: /api/errors/19b2122058ee877a. Report an issue: GitHub.

Appendix: source

Thrown at seatunnel-connectors-v2/connector-mongodb/src/main/java/org/apache/seatunnel/connectors/seatunnel/mongodb/catalog/MongodbCatalog.java:185

            throws DatabaseNotExistException, CatalogException {
        throw CommonError.unsupportedOperation(name(), "drop database ");
    }

    @Override
    public void truncateTable(TablePath tablePath, boolean ignoreIfNotExists)
            throws TableNotExistException, CatalogException {
        try {
            if (!tableExists(tablePath)) {
                if (ignoreIfNotExists) {
                    return;
                }
                throw new TableNotExistException(name(), tablePath);
            }
            MongoDatabase db = mongoClient.getDatabase(tablePath.getDatabaseName());
            MongoCollection<Document> collection = db.getCollection(tablePath.getTableName());
            collection.deleteMany(new Document());
        } catch (Exception e) {
            throw new CatalogException(
                    "Failed to truncate collection: " + tablePath.getFullName(), e);
        }
    }

    @Override
    public boolean isExistsData(TablePath tablePath) {
        try {
            if (!tableExists(tablePath)) {
                return false;
            }
            MongoDatabase db = mongoClient.getDatabase(tablePath.getDatabaseName());
            MongoCollection<Document> collection = db.getCollection(tablePath.getTableName());
            return collection.estimatedDocumentCount() > 0;
        } catch (Exception e) {
            return false;
        }
    }

View on GitHub (pinned to cf67b549a7)