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 start 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>'