Volume provides writeable, persistent file storage that can be shared between one or more Modal functions. The contents of a volume are exposed as a filesystem, allowing you to share data between different functions or persist durable state across instances.
Key concepts
Unlike a networked filesystem, you need to explicitly reload the volume to see changes made since it was mounted. Similarly, you need to explicitly commit any changes you make to the volume for the changes to become visible outside the current container. Concurrent modification is supported, but concurrent modifications of the same files should be avoided. Last write wins in case of concurrent modification of the same file - any data the last writer didn’t have when committing changes will be lost. Reloading requirement: Volumes can only be reloaded if there are no open files. Attempting to reload with open files will result in an error.Basic usage
Creating volumes
Reference by name
Create or reference a named volume that persists across app runs:Ephemeral volumes
Create a temporary volume that exists only within a context manager:Create from ID
Reference a volume by its object ID:Managing volume contents
Committing changes
Reloading changes
Batch upload files
Upload multiple files or directories efficiently:force=True to overwrite existing files:
List files
List all files in a directory:Read files
Read a file from the volume:The
read_file method is primarily intended for use outside of a Modal App. When the volume is mounted on a function, use normal filesystem operations instead.Copy files
Copy files within the volume:If the volume is already mounted on a Modal function, use normal filesystem operations like
os.rename() and then commit() the volume.Remove files
Remove a file or directory:Read-only volumes
Mount a volume as read-only to prevent accidental modifications:Managing volumes
TheVolume.objects namespace provides methods for managing named volumes.
Create a volume
List volumes
List all volumes in the active environment:Delete a volume
Rename a volume
API reference
Volume methods
Commit changes to a mounted volume, persisting them in durable storage.
Make latest committed state of volume available in the running container. Will fail if there are open files.
List all files under a path prefix. Returns a list of
FileEntry objects.Parameters:path(str): Directory or file pathrecursive(bool): If True, list files recursively. Default: False
Iterate over all files in a directory. Yields
FileEntry objects.Parameters:path(str): Directory or file pathrecursive(bool): If True, iterate recursively. Default: True
Read a file from the volume. Yields bytes chunks.Parameters:
path(str): Path to the file
Remove a file or directory from the volume.Parameters:
path(str): Path to removerecursive(bool): If True, remove directories recursively. Default: False
Copy files within the volume.Parameters:
src_paths(list[str]): List of source pathsdst_path(str): Destination pathrecursive(bool): If True, copy directories recursively. Default: False
Context manager for batch-uploading files to a volume.Parameters:
force(bool): If True, overwrite existing files. Default: False
Configure Volume to mount as read-only. Returns a new Volume instance.
Return information about the Volume object. Returns a
VolumeInfo dataclass with name, created_at, and created_by fields.FileEntry
TheFileEntry dataclass represents a file or directory entry:
path(str): File pathtype(FileEntryType): FILE, DIRECTORY, SYMLINK, FIFO, or SOCKETmtime(int): Modification timesize(int): File size in bytes