apache/iceberg · error · AlreadyExistsException
Cannot create namespace %s. Namespace already exists
Error message
Cannot create namespace %s. Namespace already exists
What it means
InMemoryCatalog.createNamespace throws AlreadyExistsException when the namespace is already registered. The in-memory catalog stores namespaces in a map keyed by Namespace; duplicate creation is rejected rather than overwritten.
Source
Thrown at core/src/main/java/org/apache/iceberg/inmemory/InMemoryCatalog.java:203
if (views.containsKey(to)) {
throw new AlreadyExistsException("Cannot rename %s to %s. View already exists", from, to);
}
tables.put(to, fromLocation);
tables.remove(from);
}
}
@Override
public void createNamespace(Namespace namespace) {
createNamespace(namespace, Collections.emptyMap());
}
@Override
public void createNamespace(Namespace namespace, Map<String, String> metadata) {
synchronized (this) {
if (namespaceExists(namespace)) {
throw new AlreadyExistsException(
"Cannot create namespace %s. Namespace already exists", namespace);
}
namespaces.put(namespace, ImmutableMap.copyOf(metadata));
}
}
@Override
public boolean namespaceExists(Namespace namespace) {
return namespaces.containsKey(namespace);
}
@Override
public boolean dropNamespace(Namespace namespace) throws NamespaceNotEmptyException {
synchronized (this) {
if (!namespaceExists(namespace)) {
return false;
}View on GitHub (pinned to 86d9c8fc54)
Solutions
- Check namespaceExists(namespace) before calling createNamespace
- Wrap in try-catch for AlreadyExistsException for idempotent setup
- Use a unique namespace name per test/run
- Clear or recreate the catalog between runs
Example fix
// before
catalog.createNamespace(Namespace.of("test_ns"));
// after
Namespace ns = Namespace.of("test_ns");
if (!catalog.namespaceExists(ns)) {
catalog.createNamespace(ns);
} Defensive patterns
Strategy: validation
Validate before calling
Namespace ns = Namespace.of("test_ns");
if (!catalog.namespaceExists(ns)) { catalog.createNamespace(ns, metadata); } Try / catch
try { catalog.createNamespace(ns, metadata); } catch (AlreadyExistsException e) { /* idempotent: ignore */ } Prevention
- Guard creation with namespaceExists
- Write idempotent setup helpers used by all tests
- Isolate catalog instances per test run
- Avoid assuming create-if-not-exists semantics
When it happens
Trigger: Calling catalog.createNamespace(ns) or createNamespace(ns, metadata) twice for the same namespace, including creating the root-level namespace that already exists. The nested createNamespace overload delegates here, so both entry points can throw.
Common situations: Test setup run twice against a shared catalog instance; idempotent bootstrap scripts that assume create-if-not-exists semantics; parallel test classes initializing the same namespace.
Understand the failure class
Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.
Related errors
- Cannot create namespace %s because it already exists in Glue
- Namespace already exists: %s
- Namespace already exists: %s
- Cannot list tables for namespace. Namespace does not exist:
- Cannot rename %s to %s. Namespace does not exist: %s
AI-assisted analysis of apache/iceberg@86d9c8fc54 (2026-09-12).
Data as JSON: /api/errors/79ee7b1cd9ad407a.
Report an issue: GitHub.