KB · Oracle Database · Script

"ORA-03297: file contains used data beyond requested RESIZE value": shrink datafiles to the high-water mark

Oracle Database · Script · Verified on Oracle 23ai · Updated 2026-12-03

Error this answers

  • ORA-03297: file contains used data beyond requested RESIZE value

After a big purge, a tablespace can be mostly free while its datafiles stay large. You can only shrink a datafile down to its last used extent (its high-water mark); ask for less and Oracle refuses with:

ORA-03297: file contains used data beyond requested RESIZE value

This script works out, for every file in a tablespace, the size that ends exactly at its last used extent, and writes the RESIZE statements to a file for you to review and run.

Usage

SQL> @dbf_trim.sql USERS
SQL> host cat dbf_trim.run
SQL> @dbf_trim.run

Nothing is resized until you run dbf_trim.run.

dbf_trim.sqlMIT · Download ·
-- dbf_trim.sql: generate RESIZE statements that shrink each datafile to its last used extent
-- Copyright (c) HASHJOIN. MIT License (https://hashjoin.com/kb/license/)
-- https://hashjoin.com/kb/oracle/shrink-datafiles-to-high-water-mark/
--
-- Usage: @dbf_trim.sql <tablespace>
--        review dbf_trim.run, then @dbf_trim.run
--
-- Writes the statements to dbf_trim.run and stops: nothing is resized until you run that file.

set verify off
set trims on
set pages 0
set feedback on
set lines 300


set feedback off
set echo off
set timing off

SELECT /*+ ORDERED */
'ALTER DATABASE DATAFILE '||chr(39)||dbf.file_name||chr(39)||
   ' RESIZE '||LTRIM(TO_CHAR((dbf.bytes-free.bytes)/1024,'999999999999'))||'K ;'
FROM (SELECT fr.file_id
      ,      fr.bytes
      FROM   dba_free_space fr
      WHERE  (file_id, block_id) IN
                         (SELECT file_id, MAX(BLOCK_ID)
                          FROM   dba_free_space
                          WHERE  tablespace_name = UPPER('&&1')
                          GROUP BY file_id)
      AND  TABLESPACE_NAME = UPPER('&&1')
      AND  block_id > (SELECT MAX(block_id)
                       FROM dba_extents
                       WHERE file_id = fr.file_id
                         and TABLESPACE_NAME = UPPER('&&1'))) free
,    dba_data_files dbf
WHERE free.file_id = dbf.file_id


spool dbf_trim.run
/
spool off

set feedback on
set timing on
prompt
prompt Wrote dbf_trim.run. Review it, then run it:  @dbf_trim.run
undefine 1

curl -O https://hashjoin.com/kb/oracle/shrink-datafiles-to-high-water-mark/dbf_trim.sql

Reading the output

Each line of dbf_trim.run is one ALTER DATABASE DATAFILE '…' RESIZE nK;. In a lab run on a 32 MB file whose tail had been freed by dropping a table, it wrote:

ALTER DATABASE DATAFILE '/opt/oracle/oradata/FREE/FREEPDB1/kb_trim01.dbf' RESIZE 6400K ;

and after running it the file was 6.25 MB. Files whose free space isn't at the end (a used extent sits at the end of the file) get no statement, because they can't shrink at all.

Gotchas

What it reads

dba_free_space dba_extents dba_data_files

Tired of fixing the same thing twice?

HASHJOIN turns the checks and fixes your on-call runs by hand into tooling that gives the same answer every time. Fixed scope, and you own all of the code.