# PostgreSQL DROP VIEW IF EXISTS

> If a VIEW to be dropped doesn’t exist, then Postgres throws a VIEW doesn’t exist error. To rectify this error, the “IF EXISTS” option is used with the DROP VIE…

PostgreSQL provides a **DROP VIEW** statement that is used to delete/drop single or multiple views from the database. However, if the specified **VIEW** doesn’t exist, then Postgres throws a **VIEW** doesn’t exist error. The “ **IF EXISTS** ” option is used with the **DROP VIEW** statement in Postgres to rectify this error.

This article will present a detailed overview of the **DROP VIEW** statement with suitable examples. So, let’s start!

 **How to Use DROP VIEW IF EXISTS Statement in Postgres?**

To use the DROP VIEW statement in Postgres, users must follow the below syntax:
    
    
    DROP VIEW IF EXISTS view_name 
    CASCADE | RESTRICT;

Let’s understand the above statement step-by-step:

\- **View_name** represents a view to be deleted.  
\- The “ **IF** **EXISTS** ” is an optional clause used to avoid the view does not exist error.  
\- **CASCADE** is an optional clause that allows us to drop a view and any objects that depend on it. If you do not specify the **CASCADE** clause and some objects depend on the view, the DROP VIEW statement causes an error.  
\- **RESTRICT** is an optional clause that prevents the DROP VIEW statement from deleting the view if some objects depend on it.

Through practical examples, let’s comprehend the working of the **DROP VIEW IF EXISTS** statement. But before that, we will understand what would happen if we didn’t specify the IF EXISTS option.

 **Example 1: How to Drop a View in Postgres?**

Execute the below-provided statement to delete a view named “example_view”:
    
    
    DROP VIEW example_view;

The output shows that the targeted view has been dropped successfully. Let’s execute the DROP VIEW statement one more time to see how it works:
    
    
    DROP VIEW example_view;

The output shows an error when we try to drop a view that doesn’t exist.

 **Example 2: How to Use the IF EXISTS Option With the DROP VIEW Statement in Postgres?**

Let’s learn how the IF EXISTS option deals with the “view doesn’t exist” error:
    
    
    DROP VIEW IF EXISTS example_view;

The output snippet verifies that the IF EXISTS option shows a notice instead of throwing an error.

That’s it from this Postgres guide!

 **Conclusion**

In PostgreSQL, the **DROP VIEW** statement is used to delete/drop single or multiple views from the database. However, if the specified **VIEW** doesn’t exist, then Postgres throws a **VIEW** doesn’t exist error. To rectify this error, users must use the “ **IF EXISTS** ” option with the **DROP VIEW** statement. This Postgres blog explained the working of the “DROP VIEW IF EXISTS” option with practical examples.

---
[View this page online](https://www.commandprompt.com/education/postgresql-drop-view-if-exists/)