Skip to content

Vault

Official

Work with files and folders stored inside a vault.

Import:

import { Vault } from 'obsidian';

Signature:

export class Vault extends Events

Extends: Events

Constructor

new Vault(adapter: DataAdapter)

Constructor.

To get the constructor instance, use getVaultConstructor from obsidian-typings/implementations.

Properties

PropertyTypeDescription
_Record<string, EventsEntry[]>Internal storage of registered event handlers by event name.
(Inherited from Events)
adapterDataAdapterThe low-level adapter of the vault.
cacheLimitnumberMax size of the cache in bytes
configAppVaultConfigObject containing all config settings for the vault (editor, appearance, ... settings).
configDirstringGets the path to the config folder. This value is typically .obsidian but it could be different.
configTsnumberTimestamp of the last config change
fileMapVaultFileMapRecordMapping of path to Obsidian folder or file structure
onChangeFileSystemWatchHandlerListener for all events on the vault
requestSaveConfigDebouncer<[], Promise<void>>Debounced function for saving config.
rootTFolderThe same obsidian#TFolder object as .fileMap["/"]

Methods

MethodReturnsDescription
addChild(file)voidAdd file as child/parent to respective folders
append(file, data, options?)Promise<void>Add text to the end of a plaintext file inside the vault.
appendBinary(normalizedPath, data, options?)Promise<void>Add data to the end of a binary file inside the vault.
cachedRead(file)Promise<string>Read the content of a plaintext file stored inside the vault. Use this if you only want to display the content to the user. If you want to modify the file content afterward use Vault.read
checkForDuplicate(file, newPath)booleanCheck whether new file path is available
checkPath(path)booleanCheck whether path has valid formatting (no dots/spaces at end, ...)
copy(file, newPath)Promise<T>Create a copy of a file or folder.
create(path, data, options?)Promise<TFile>Create a new plaintext file inside the vault.
createBinary(path, data, options?)Promise<TFile>Create a new binary file inside the vault.
createFolder(path)Promise<TFolder>Create a new folder inside the vault.
delete(file, force?)Promise<void>Deletes the file completely.
deleteConfigJson(configFile)Promise<void>Remove a vault config file
exists(path, isCaseSensitive?)Promise<boolean>Check whether a path exists in the vault.
generateFiles(e, t)Promise<void>Generate and register files from an async generator.
getAbstractFileByPath(path)TAbstractFile | nullGet a file or folder inside the vault at the given path. To check if the return type is a file, use instanceof TFile. To check if it is a folder, use instanceof TFolder.
getAbstractFileByPathInsensitive(path)null | TAbstractFileGet an abstract file by path, insensitive to case.
getAllFolders(includeRoot?)TFolder[]Get all folders in the vault.
getAllLoadedFiles()TAbstractFile[]Get all files and folders in the vault.
getAvailablePath(path, extension)stringGet path for file that does not conflict with other existing files
getAvailablePathForAttachments(filename, extension, file)Promise<string>Get path for attachment that does not conflict with other existing files
getConfig(string)unknownGet value from config by key.
getConfigFile(configFile)stringGet path to config file (relative to vault root).
getDirectParent(file)null | TFolderGet direct parent of file.
getFileByPath(path)TFile | nullGet a file inside the vault at the given path.
getFiles()TFile[]Get all files in the vault.
getFolderByPath(path)TFolder | nullGet a folder inside the vault at the given path.
getMarkdownFiles()TFile[]Get all Markdown files in the vault.
getName()stringGets the name of the vault.
getResourcePath(file)stringReturns a URI for the browser engine to use, for example to embed an image.
getRoot()TFolderGet the root folder of the current vault.
isEmpty()booleanCheck whether files map cache is empty
iterateFiles(files, cachedRead)voidIterate over the files and read them
load()Promise<void>Load vault adapter
modify(file, data, options?)Promise<void>Modify the contents of a plaintext file.
modifyBinary(file, data, options?)Promise<void>Modify the contents of a binary file.
off(name, callback)voidRemove an event listener.
(Inherited from Events)
offref(ref)voidRemove an event listener by reference.
(Inherited from Events)
on('create', callback, ctx?)EventRefCalled when a file is created. This is also called when the vault is first loaded for each existing file If you do not wish to receive create events on vault load, register your event handler inside Workspace.onLayoutReady.
on('modify', callback, ctx?)EventRefCalled when a file is modified.
on('delete', callback, ctx?)EventRefCalled when a file is deleted.
on('rename', callback, ctx?)EventRefCalled when a file is renamed.
on('config-changed', callback, ctx?)EventRefCalled whenever any of Obsidian's settings are changed.
on('raw', callback, ctx?)EventRefTriggered whenever a file gets loaded internally
on(name, callback, ctx?)EventRefAdd an event listener.
(Inherited from Events)
process(file, fn, options?)Promise<string>Atomically read, modify, and save the contents of a note.
read(file)Promise<string>Read a plaintext file that is stored inside the vault, directly from disk. Use this if you intend to modify the file content afterwards. Use Vault.cachedRead otherwise for better performance.
readBinary(file)Promise<ArrayBuffer>Read the content of a binary file stored inside the vault.
readConfigJson(config)Promise<null | object>Read a config file from the vault and parse it as JSON.
readJson(path)Promise<null | object>Read a config file (full path) from the vault and parse it as JSON.
readPluginData(path)Promise<null | object>Read a plugin config file (full path relative to vault root) from the vault and parse it as JSON.
readRaw(path)Promise<string>Read a file from the vault as a string.
recurseChildren(root, cb)void
reloadConfig()voidReload all config files
removeChild(file)voidRemove file as child/parent from respective folders.
rename(file, newPath)Promise<void>Rename or move a file. To ensure links are automatically renamed, use FileManager.renameFile instead.
resolveFilePath(path)null | TAbstractFileGet the file by absolute path.
resolveFileUrl(url)null | TAbstractFileGet the file by Obsidian URL
saveConfig()Promise<void>Save app and appearance configs to disk
setConfig(key, value)voidSet value of config by key.
setConfigDir(configDir)voidSet where the config files are stored (relative to vault root).
setFileCacheLimit(limit)voidSet file cache limit
setupConfig()Promise<void>Load all config files into memory
trash(file, system)Promise<void>Tries to move to system trash. If that isn't successful/allowed, use local trash.
trigger(name, data?)voidTrigger an event, executing all the listeners in order even if some of them throw an error.
(Inherited from Events)
tryTrigger(evt, args)voidTry to trigger an event, executing all the listeners in order even if some of them throw an error.
(Inherited from Events)
writeConfigJson(config, data, pretty?)Promise<void>Write a config file to disk.
writeJson(path, data, pretty?)Promise<void>Write a config file (full path) to disk.
writePluginData(path, data, pretty?)Promise<void>Write a plugin config file (path relative to vault root) to disk.

Links to this page: