Cannot drop schema because it is being referenced by object

In this Azure SQL tutorial, I will explain how I encountered the error “cannot drop schema because it is being referenced by object” and how did I resolve it.

Cannot drop schema because it is being referenced by object

I was trying to delete a schema in Azure SQL database using the DROP SCHEMA statement. Below is the T-SQL code that I was trying to execute:

DROP SCHEMA Production

Production was the schema name. After executing the above query I got the error “failed to execute query. Error: Cannot drop schema ‘Production’ because it is being referenced by object ‘Items’” as shown below in the image:

Cannot drop schema because it is being referenced by object
Cannot drop schema because it is being referenced by object

Solution: Cannot drop schema because it is being referenced by object

I searched about this error on the web and found the reason for this error message.

We cannot drop a schema until all the objects under the schema are not deleted.

In my case, the Items object was a table created under the Production schema.

Therefore I firstly dropped all the objects like tables, stored procedures, and functions under the Production schema. Then I executed the query to drop the Production schema and the query got executed successfully.

Cannot drop schema because it is being referenced
Dropped the schema successfully

In this way. I was able to resolve the error “Cannot drop schema because it is being referenced by object“. It will also fix, cannot drop schema because it is being referenced by object sql server error.

Related Azure SQL tutorials: