How to use the FreeSpace query of the SYS.Database class to check the free space on the disk where the database is located
InterSystems FAQ rubric
You can check the free disk space at any time using the system utility class: SYS.Database and query: FreeSpace.
Here's how to try it in the IRIS terminal (go to the %SYS namespace and then run it):
zn"%SYS"set stmt=##class(%SQL.Statement).%New()
set st=stmt.%PrepareClassQuery("SYS.Database","FreeSpace")
set rset=stmt.%Execute()
do rset.%Display()The output result example is as follows:
*In the command execution example, all databases are located on the same disk, so the free disk space (DiskFreeSpace) returns the same value.
Dumping result #1
DatabaseName Directory MaxSize Size ExpansionSize AvailableFreeDiskFreeSpace Status SizeInt AvailableNum DiskFreeSpaceNum ReadOnly
IRISSYS c:\intersystems\irishealth3\mgr\ 無制限 159MB システムデフォル ト 18MB 11.32 245.81GB マウント/RW 159 18 2517050
ENSLIB c:\intersystems\irishealth3\mgr\enslib\ 無制限 226MB システムデフォル ト 19MB 8.4 245.81GB マウント/R 226 19 2517051
<一部省略>
IRISTEMP c:\intersystems\irishealth3\mgr\iristemp\ 無制限 51MBシス テムデフォルト 49MB 96.07 245.81GB マウント/RW 51 49251705 0
USER c:\intersystems\irishealth3\mgr\user\ 無制限 31MB システムデフォル ト 8.5MB 27.41 245.81GB マウント/RW 31 8.5 2517050If you want to specify the database directory you want to refer to, run the following:
//Use the $LISTBUILD() function to obtain the full path of the database directory you want to view.set dbdir=$LISTBUILD("c:\intersystems\irishealth3\mgr","c:\intersystems\irishealth3\mgr\user")
set rset=stmt.%Execute(dbdir)
do rset.%Display()If you want to get only the Database Name (DatabaseName), Current Size (Size) in MB, Available Space (Available) in MB, Free Space (Free), and Disk Free Space (DiskFreeSpace) in a specified database directory, follow the steps below (create a routine/class in VSCode or Studio while connected to the %SYS namespace and write the code).
Class ZMyClass.Utils
{
ClassMethod GetDiskFreeSpace()
{
set dbdir=$LISTBUILD("c:\intersystems\irishealth3\mgr","c:\intersystems\irishealth3\mgr\user")
set stmt=##class(%SQL.Statement).%New()
set st=stmt.%PrepareClassQuery("SYS.Database","FreeSpace")
set rset=stmt.%Execute(dbdir)
while(rset.%Next()) {
write rset.%Get("DatabaseName")," - ",
rset.%Get("Size")," - ",rset.%Get("Available")," - ",
rset.%Get("Free"),"% - ",rset.%Get("DiskFreeSpace"),!
}
}
}
NOTE: If you place user-defined routines or classes in the %SYS namespace, creating them with names beginning with Z ensures that the user-defined source code remains available after an upgrade installation.
An example of execution is as follows.
USER>zn"%SYS"%SYS>do##class(ZMyClass.Utils).GetDiskFreeSpace()
IRISSYS - 159MB - 18MB - 11.32% - 245.81GB
USER - 31MB - 8.5MB - 27.41% - 245.81GB
%SYS>
Comments
@Mihoko Iijima This tip is really cool, you can also run it using SQL
select * from SYS.Database_FreeSpace() where DatabaseName = 'USER'.png)