Managing Snowflake virtual warehouses with Python

You can use Python to manage Snowflake virtual warehouses, which are clusters of compute resources in Snowflake. For an overview of warehouses, see Virtual warehouses.

The Snowflake Python API represents warehouses with two separate types:

  • Warehouse: Exposes a warehouse’s properties such as its name, size, type, and auto-resume and auto-suspend settings.

  • WarehouseResource: Exposes methods you can use to fetch a corresponding Warehouse object, suspend and resume the warehouse, and delete the warehouse.

Prerequisites

The examples in this topic assume that you’ve added code to connect with Snowflake and to create a Root object from which to use the Snowflake Python API.

For example, the following code uses connection parameters defined in a configuration file to create a connection to Snowflake:

from snowflake.core import Root
from snowflake.snowpark import Session

session = Session.builder.config("connection_name", "myconnection").create()
root = Root(session)
Copy

Using the resulting Session object, the code creates a Root object to use the API’s types and methods. For more information, see Connect to Snowflake with the Snowflake Python API.

Creating a warehouse

To create a warehouse, first create a Warehouse object, and then create a WarehouseCollection object from the API Root object. Using WarehouseCollection.create, add the new warehouse to Snowflake.

Code in the following example creates a Warehouse object that represents a warehouse called my_wh:

from snowflake.core import Root
from snowflake.core.warehouse import Warehouse

my_wh = Warehouse(
  name="my_wh",
  warehouse_size="SMALL",
  auto_suspend=600,
)
warehouses = root.warehouses
warehouses.create(my_wh)
Copy

The code creates a WarehouseCollection variable warehouses and uses WarehouseCollection.create to create a new warehouse in Snowflake.

Getting warehouse details

You can get information about a warehouse by calling the WarehouseResource.fetch method, which returns a Warehouse object.

Code in the following example gets information about a warehouse called my_wh:

from snowflake.core import Root
from snowflake.core.warehouse import Warehouse

my_wh = root.warehouses["my_wh"].fetch()
print(my_wh.to_dict())
Copy

Listing warehouses

You can list warehouses using the WarehouseCollection.iter method, which returns a PagedIter iterator of Warehouse objects.

Code in the following example lists warehouses whose name includes the text my and prints the name of each:

from snowflake.core import Root
from snowflake.core.warehouse import Warehouse, WarehouseCollection

warehouses: WarehouseCollection = root.warehouses
wh_iter = warehouses.iter(like="my%")  # returns a PagedIter[Warehouse]
for wh_obj in wh_iter:
  print(wh_obj.name)
Copy

Performing warehouse operations

You can perform common warehouse operations—such as suspending and resuming warehouses and aborting all queries on warehouses—with a WarehouseResource object.

Code in the following example suspends and resumes the my_wh warehouse, aborts all running or queued queries on the warehouse, and then deletes the warehouse:

from snowflake.core import Root
from snowflake.core.warehouse import Warehouse

my_wh_res = root.warehouses["my_wh"]

my_wh_res.suspend()
my_wh_res.resume()
my_wh_res.abort_all_queries()
my_wh_res.delete()
Copy