How to Remove and Drop Datafiles from Tablespace in Oracle Database

Oracle RDBMS databases stores data logically in the form of tablespaces and physically in the form of data files. The datafiles are added to the tablespaces as database space requirement grows bigger. However, there are several reasons you may want to remove or delete data files from a tablespace. Such as accidentally add a wrongly sized or unwanted datafile to a tablespace, or the data space usage has became smaller and some data files want to be removed, or attempt to recover Oracle database which fails to start due to missing or corrupted datafiles by removing them, Oracle does not provide an easy way or user interface to delete or drop datafiles from a tablespace. Once a datafile is made part of a tablespace, it can no longer be detached or removed from the tablespace, albeit there are several workarounds.

How to Completely Drop the Whole Tablespace with All Datafiles

The easiest way to drop a or multiple datafiles is by dropping the entire tablespace together with its datafiles. If you no longer need the data contents of the tablespace, the following command will drop the tablespace, the datafile, and the tablespace’s contents from the data dictionary. All of the objects that where contained in that tablespace are permanently removed.

DROP TABLESPACE <tablespace name> INCLUDING CONTENTS AND DATAFILES;

If you don’t specify “AND DATAFILES”, Oracle will not drop the physical datafile after the DROP TABLESPACE command, but you can always delete the files from operating system shell (If the data files are locked, restart the server).

But always perform a backup of the database, as the “DROP TABLESPACE” command is irreversible. It’s also a good practice to check and identify how many datafiles a tablespace actually has before performing the drop action in order to avoid mistake.

How to Check How Many Datafiles a Tablespace Has

To determine and identify all datafiles that link to a tablespace, use the following query, with tablespace name in capital letter:

SELECT file_name, tablespace_name
FROM dba_data_files
WHERE tablespace_name =’<tablespace name>’;

If a tablespace contains multiple datafiles, and you just want to drop one or some of the datafiles and keep the remaining datafiles together with the objects and contents, the objects or data must be exported for the affected table space. Once exported, the tablespace can be dropped with above “DROP TABLESPACE” command. Then, recreate the tablespace with the datafile(s) required (that you initially want to keep), and then import the objects into the recreated tablespace.

If one or more datafiles is missing after a recovery process or accidental deletion, you can use ALTER DATABASE DATAFILE <datafile name> OFFLINE DROP command to make the datafile offline so that database can starts up after which the troubled tablespace can be dropped. (See instruction to recover from missing datafiles.)

How to Resize a Datafile to Minimum Size

Another alternative to drop the datafile is by shrinking the size of datafile instead of dropping. This option is only possible if there is no extents in the datafile. If there are none, it’s possible to resize the data file down to a very small file (2 blocks), where Oracle database will no longer create any extent in the datafile. However, this workaround does not remove the datafile from tablespace nor delete the file physically, but it reduce the risk as no data will be truncated as only empty blocks are reduced. It simply makes it unusable and takes up almost close to no disk space. To resize a datafile, use the following query:

alter database datafile ‘<datafile name>’ resize 8M;

How to Determine the Extents inside Datafile

To check and determine the extents that exists in a datafile, use this query:

SELECT owner, segment_name
FROM dba_extents a, dba_data_files b
WHERE a.file_id = b.file_id
AND b.file_name = ‘<datafile name>’

Share and contribute or get technical support and help at My Digital Life Forums.



One Response to “How to Remove and Drop Datafiles from Tablespace in Oracle Database”

  1. How Drop Tablespace and Recover Oracle Database When Accidentally Delete Datafile » My Digital Life
    August 4th, 2007 02:55
    1

    [...] Tip: How to check datafiles that belong to tablespace. [...]

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe without commenting


Custom Search

New Articles

Incoming Search Terms for the Article

oracle drop datafile - oracle drop tablespace - drop datafile oracle - oracle remove datafile - drop datafile - DELETE DATAFILE - drop tablespace in oracle - remove datafile - drop tablespace oracle - Drop datafiles - drop oracle datafile - drop tablespace datafile - delete tablespace - delete datafile oracle - remove datafile oracle - oracle delete datafile - oracle tablespace delete - oracle tablespace drop datafile - oracle drop tablespace datafile - How to drop datafile in oracle - delete tablespace oracle - drop tablespace including contents and datafiles - drop datafiles oracle - remove datafile from tablespace - drop oracle tablespace - alter tablespace remove datafile - remove tablespace - drop tablespace including datafiles - drop tablespace command - drop tablespace file - Drop tablespace and datafile in oracle - delete datafiles - oracle drop datafile; - oracle drop temporary tablespace - drop tablespace and datafile - drop a datafile - how to drop tablespace in oracle - Oracle tablespace remove datafile - oracle alter database drop datafile - command to drop datafile - oracle removing data file - Oracle Drop tablespace command - delete oracle tablespace - drop datafile in oracle - drop tablespace + Oracle - datafile drop - alter tablespace remove - drop tablespace and datafiles - DROP TABLESPACE tablespace_name [INCLUDING CONTENTS [AND DATAFILES] - how to delete tablespace -