Export.json file fails with "create failed for create-3"

Your question

After creating an export.json file, I am trying to apply it;

Plan: 0 destroy, 7 update, 6 create (39 objects)
✓ created Tenant (3)
✗ create Domain: Domain: create failed for `create-3` (operation #2): error: primaryKeyViolation |   Properties: name |   Object id:   Domain#b
Done: 0 destroyed, 0 updated, 3 created (1 failed)
error: Domain: create failed for `create-3` (operation #2): error: primaryKeyViolation |   Properties: name |   Object id:   Domain#b

It appears corrupted - should I regenerate it?

The error is not file corruption – it is a well-documented behavior of stalwart-cli apply. The plan stopped partway through and when you re-run it, the objects that were already successfully created (including the Tenant and one or more Domains) now exist in the server, so a second create attempt hits the server’s primary-key constraint.

Here is what is happening and how to fix it:

apply is not idempotent for create-only plans. The first run created 3 Tenants before failing. On the second run those Tenants already exist, and attempting to create Domain#b (which was also committed) raises primaryKeyViolation.

Fix options:

  1. Pair each create with a leading destroy. The docs describe this as the idiomatic pattern – a destroy of the same type runs first (in reverse order), clearing existing objects, then the create pass rebuilds them. The stalwart-cli snapshot command emits plans in exactly this shape.

  2. Use --continue-on-error cautiously. If the objects that already committed are correct as-is, you could re-run with --continue-on-error so the CLI skips past the already-created objects and attempts the remaining ones. Bear in mind this will still fail on every already-existing object; you would need to confirm manually that those are correct.

  3. Delete the already-committed objects manually using stalwart-cli delete Domain <id> (and Tenants as needed), then re-run the original plan cleanly.

The underlying cause is almost always that export.json does not include destroy operations before its create operations. If you generated this file via stalwart-cli snapshot, check whether the export included both the destroy and create passes. If you wrote or edited the file manually, add a {"@type":"destroy","object":"Domain"} (scoped with a filter if needed) and equivalent for Tenant before the create block.

See the declarative bulk operations (apply) docs – specifically the “Idempotent re-runs” section – for the full explanation.

This is an automated reply from the Stalwart Help Bot. Other community members may follow up if this answer is incomplete or wrong.

The file is almost certainly not corrupted. primaryKeyViolation on
Domain#b means the registry already has a Domain with that name from a
previous apply run; the recovery procedure in UPGRADING/v0_16.md (the
“Recovering from a partial apply” section) covers it.

The short version: while still in recovery mode, delete the registry-only
object types (Domain, Tenant, DkimSignature, Certificate) that were
partially created, then re-run apply. Concretely:

stalwart-cli query Domain | jq -r ‘..id’ | stalwart-cli delete Domain --stdin
stalwart-cli query Tenant | jq -r ‘..id’ | stalwart-cli delete Tenant --stdin

Important: do NOT delete Account; that would lose the imported mail data.

Then re-run stalwart-cli apply --file export.json.