Saturday, March 29, 2014

Destroy a schema from the inside

Oracle does not provide a way for a user to pave over his own schema.  If you don't have dba privileges, this means you have to remove your schema by destroying each object.

This is a VERY rough recipe that I just wrote for a coworker.  It assumes you have Oracle SQLDeveloper and a shell handy.
  1. sql> select /*csv*/ 'DROP ',user_objects.object_type,' ',user_objects.object_name from user_objects where object_type in ('INDEX','TABLE');
  2. F5 (run as script)
  3. Select everything in your output window and copy it to /tmp/objects
  4. bash> cat /tmp/objects | grep -v OBJECT_NAME | sed s/\"//g | sed s/\,//g | sed s/$/\;/ > /tmp/drop.sql
  5. In SQLNavigator, clear your query and output screens (pencil eraser)
  6. File -> Open /tmp/drop.sql
  7. F5 (run as script) -- drops may cascade, which will cause errors later in the script; ignore
  8. sql> purge recyclebin;
  9. sql> select * from user_objects;  -- should return 0 rows now.
This can easily be adapted to whatever Oracle client and text editor you have handy.  In place of the seds and greps, use a basic text editor to remove the header line, remove all commas and quotes, and add a semicolon to each line.