In my open-source database migration project Flyway, I have a feature that cleans all objects in the current database schema, without dropping the schema itself.
The typical implementation works as follows:
Oracle Spatial Indexes have been causing me a lot of grief though.
How can I reliably enumerate them in order to produce DROP INDEX xyz statements?
Note: This must work on both XE, 10g and 11g. All references in the MDSYS schema must be gone.
My current solution looks like this:
On XE:
On Oracle 10g:
10g seems to cascade the removal of the metadata in MDSYS.sdo_index_metadata_table and the removal of the spatial index tables (MDRT_1234$ and the like).
XE doesn't.
Both 10g and XE don't cascade the removal of the metadata in MDSYS.user_sdo_geom_metadata
I solved it by enumerating all SPATIAL indexes using
select INDEX_NAME from USER_SDO_INDEX_INFO
And using INDEX_NAME to generate DROP statements like
DROP INDEX my_index