Dropping everything from an Oracle database

Contributor Icon Contributed by u02sgb Date Icon October 19, 2004  
Tag Icon Tagged: Oracle

If you don’t want to create your database from scratch it’s sometimes useful to just delete all the objects from it.


1. Put the following in the file “DBdrop.sql”


set feedback off
set pagesize 0
spool AllObjectsDrop.sql
select 'drop view '||view_name||';' from user_views;
select distinct 'drop sequence '||sequence_name|| ';'from user_sequences;
select distinct 'drop table '||table_name|| ';'from user_tables;
select distinct 'drop procedure '||name|| ';'from user_source where type = 'procedure';
select distinct 'drop function '||name|| ';'from user_source where type = 'function';
select distinct 'drop package '||name|| ';'from user_source where type = 'package';
select 'drop synonym '||synonym_name||';' from user_synonyms where synonym_name not like 'sta%' and synonym_name like 's_%'
spool off

2. Log into the database as the user for the database you want to drop
3. Run the script (@DBdrop.sql). This creates the file “AllObjectsDrop.sql” which contains commands to drop all objects.
4. Run the created script (@AllObjectsDrop.sql)

Watch in growing panic because you didn’t mean to press return :). Oh, and I’m not responsible blah, blah…..

Previous recipe | Next recipe |
 
  • ptigga
    Just a warning: On Oracle 10g this made my database completely unusable. I couldn't even login using SYSDBA. I could stop it but not start it again.

    It didn't matter too much because it was a development database that I wanted to drop the data from, but it did put the database in such a state that I couldn't reload a new file into it using imp.

    Instead I've had to use the Database Configuration Assistant to create a new blank database again.
  • u02sgb
    Ok, sorry about that :oops: . I used to use this to wipe a database before reloading a backup. I've changed it so your no longer logging in as sys. I think the original way probably wiped out your data dictionary, Doh!
  • Anonymous
    This is a vary bad idea. There are a number of schemas besides SYS and SYSTEM that need to be intact to have a usable database. It takes about 30 seconds to make a new blank database, much faster then dropping everything. Remove the database, rebuild it and reload your export.
  • alex
    or you can drop specific users and all it's objects using:

    DROP USER <user_name> CASCADE
  • fabiyiolawale
    I'm loving this..I am creating a database for an application called POSSE..Everytime i miss out a step or an error occur,i'll need to do that back again(i mean delete the DB and create a new 1)..now diz just helped me..nice work mate..cheers
blog comments powered by Disqus