in snowflake, do you know how to drop internal stage quickly. I have 13k internal stages created in the schema, and there are many tables too. I'd like to quickly drop all the stages but not impact other objects like table or functions
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To quickly drop all internal stages in a schema without affecting other objects (tables, functions, etc.), you can use a dynamic SQL approach. Here's how to do it efficiently:
Option 1: Using INFORMATION_SCHEMA with Dynamic SQL
sql
-- Generate and execute DROP STAGE statements for all internal stages in a schema
BEGIN
LET drop_commands ARRAY := (
SELECT ARRAY_AGG('DROP STAGE IF EXISTS ' || STAGE_SCHEMA || '.' || STAGE_NAME || ';')
FROM INFORMATION_SCHEMA.STAGES
WHERE STAGE_SCHEMA = 'YOUR_SCHEMA_NAME'
AND STAGE_TYPE = 'INTERNAL'
);
FOR i IN 1 TO ARRAY_SIZE(drop_commands) DO
EXECUTE IMMEDIATE drop_commands[i-1];
END FOR;
END;
Option 2: Using Snowflake Scripting (more verbose but clearer)
sql
DECLARE
stage_cursor CURSOR FOR
SELECT STAGE_SCHEMA, STAGE_NAME
FROM INFORMATION_SCHEMA.STAGES
WHERE STAGE_SCHEMA = 'YOUR_SCHEMA_NAME'
AND STAGE_TYPE = 'INTERNAL';
drop_sql STRING;
BEGIN
FOR stage_rec IN stage_cursor DO
drop_sql := 'DROP STAGE IF EXISTS ' || stage_rec.STAGE_SCHEMA || '.' || stage_rec.STAGE_NAME;
EXECUTE IMMEDIATE :drop_sql;
END FOR;
END;
Important Notes:
Replace 'YOUR_SCHEMA_NAME' with your actual schema name
The STAGE_TYPE = 'INTERNAL' filter ensures you only drop internal stages
These scripts will only drop stages, leaving tables, functions, and other objects untouched
For 13k stages, this may take some time to execute
Consider running this in batches if you encounter timeout issues
Alternative for Very Large Numbers of Stages:
If the above methods are too slow, you could:
First generate the drop statements to a result set
Export them to a file
Then run them in batches of 100-1000 at a time
sql
-- Generate the drop statements
SELECT 'DROP STAGE IF EXISTS ' || STAGE_SCHEMA || '.' || STAGE_NAME || ';' AS DROP_COMMAND
FROM INFORMATION_SCHEMA.STAGES
WHERE STAGE_SCHEMA = 'YOUR_SCHEMA_NAME'
AND STAGE_TYPE = 'INTERNAL';