Replace files and attach metadata
These examples use the latest stable release of the Python SDK.
A file keeps its document ID, external_id, metadata and folder when you replace its contents. client.documents replaces and deletes files by ID and checks that nobody changed them since you read them. To change part of a file, edit it in place.
Replace the version you read
from flashdrive import Flashdrive
with Flashdrive(drive_id="DRIVE_UUID") as client:
observed = client.documents.get("DOCUMENT_UUID")
receipt = client.documents.replace_file(
"DOCUMENT_UUID",
"policy-v2.pdf",
expected_revision=observed.latest_revision,
idempotency_key="refund-policy-edition-2",
)
print(receipt.document_id, receipt.status)
documents.replace_file(document_id, path, *, expected_revision, wait=True, timeout=600, idempotency_key=None, drive_id=None) uploads path as the file's new contents. It copies the file's external_id, metadata and folder to the new contents; facts the service reads from a stored file, such as media. keys, are read again from the new file. It returns one UploadReceipt. By default it waits until processing finishes; wait=False returns once the upload is accepted. timeout covers the whole call.
expected_revision is required: pass the latest_revision from your earlier documents.get(), or the revision from files.list, files.glob or files.read. Without it the SDK raises ValueError before sending anything. If the file changed since your read, the call raises a PreconditionFailedError with code stale_version and nothing is written. The SDK never fetches a newer revision for you; read the file again and decide whether your change still applies. Treat revisions as opaque strings.
An upload never replaces a file
Every file has an external_id, its key within the drive. client.upload() only creates files. An upload with an external_id that the drive already has is refused with code precondition_required, and nothing is written. The call raises an UploadBatchError whose failures hold that error:
with Flashdrive(drive_id="DRIVE_UUID") as client:
client.upload("policy-v1.pdf", external_id="refund-policy")
client.upload("policy-v2.pdf", external_id="refund-policy") # refused: precondition_required
To put new contents in the file, read it and call documents.replace_file() with the revision you read, as shown above.
Keep labels and location
documents.replace_file() copies the file's metadata and folder to the new contents. A replacement sent straight to the uploads API does not: its metadata replaces all of the file's metadata, and leaving folder_id out puts the file at the top of the drive. Send every label and the folder you want to keep.
Metadata is a flat object whose values are strings, numbers, booleans or null. PDF, DOCX, TXT and Markdown files take up to 64 fields and 8,000 bytes. Files stored with format file take up to 40 fields and 6,000 bytes, plus an optional title (up to 500 bytes) and tags (up to 32 strings of up to 64 bytes each). Metadata outside these limits is refused with code invalid_metadata.
Replace text added through the API
For a document your application added as text through the content API, replace the whole text:
observed = client.documents.get("DOCUMENT_UUID")
receipt = client.documents.replace_text(
"DOCUMENT_UUID",
edited_text,
expected_revision=observed.latest_revision,
idempotency_key="save-report-1",
)
print(receipt.operation_id)
documents.replace_text(document_id, text, *, expected_revision, idempotency_key=None) takes 1 to 2,000,000 UTF-8 bytes of text and keeps the document's metadata and folder. It returns a MutationReceipt; follow its operation_id with client.operations.get(receipt.operation_id). For uploaded text files, use files.edit_text.
Delete a file
observed = client.documents.get("DOCUMENT_UUID")
client.documents.delete(
"DOCUMENT_UUID",
expected_revision=observed.latest_revision,
idempotency_key="delete-old-policy-1",
)
documents.delete(document_id, *, expected_revision, idempotency_key=None) returns a MutationReceipt. As with replacement, expected_revision is required and a changed file raises stale_version. A deleted file stays in the trash for one hour. To delete a folder with everything in it, see folders.
Delete many files at once
client.documents.delete_many(
[{"id": "DOCUMENT_UUID", "expected_revision": observed.latest_revision},
("OTHER_UUID", "REVISION")],
idempotency_key="clean-up-1",
)
documents.delete_many(documents, *, idempotency_key=None) deletes up to 1,000 files in one request, all or none. Each entry is an {"id", "expected_revision"} dictionary or an (id, revision) pair, with the revision you read. A file that is missing, already deleted or at another revision refuses the whole request (stale_version for a revision), and nothing is deleted. The files go to the trash together. The returned MutationReceipt lists each file with the revision the delete left it at. Reuse idempotency_key with the same files to retry.
Read a file's details
documents.get(document_id) returns a Document, and documents.list(folder_id=None) a list of them; folder_id="root" lists the files outside folders. A Document has id, external_id, filename, source_format, source_bytes, latest_revision, folder_id, metadata, text_status, text_status_reason, created_at, updated_at, operation_id and operation_state. Each call takes drive_id to override the client's drive.
Retry or make another change
To retry a replacement whose reply was lost, repeat it with the same idempotency key, file, revision and options. For another change, read the file again and use a new key. Request keys last 24 hours. See upload recovery and errors and retries.
Flashdrive and AsyncFlashdrive have the same methods; await each call with the async client.