# Howto: drop Postgres tables matching a pattern

Suppose that your database has lots of tables, with similar names ([maybe partitions of a bigger table](https://fragland.dev/mastering-postgresql-table-partitioning)?), here's a quick way to drop them in bulk

```bash
SELECT 'drop table '||n.nspname ||'.'|| c.relname||';' as "Name" 
FROM pg_catalog.pg_class c
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','v','S','')
     AND n.nspname <> 'pg_catalog'
     AND n.nspname <> 'information_schema'
     AND n.nspname !~ '^pg_toast'
     AND pg_catalog.pg_table_is_visible(c.oid)
     AND c.relname ilike 'tbl_2015_%'
```

This query will search into the **pg\_catalog** for all the tables named *tbl\_2015\_something* and it will output a **drop** command for each one:

```bash
         Name          
-----------------------
 drop table public.tbl_2015_01;
 drop table public.tbl_2015_02;
 drop table public.tbl_2015_03;
 ......
```

Save the output to a .sql file and execute it with *psql -f yoursqlfile.sql*

*Et voilà*!
