Blog Post

Microsoft Blog for PostgreSQL
6 MIN READ

Understanding and Reclaiming Storage After DELETE in Azure Database for PostgreSQL

Shweta_Anjankar's avatar
Aug 26, 2026

Deleting millions of rows does not necessarily reduce the storage shown for an Azure Database for PostgreSQL Flexible Server. The Azure Portal storage graph/matrix often continues to display similar storage usage even after large-scale DELETE operations. This is expected PostgreSQL behavior, not a storage fault. PostgreSQL normally keeps the released space inside its data files so it can be reused. Returning that space to the operating system requires a table rewrite or a different maintenance approach.

Why DELETE Does Not Immediately Shrink Storage and Azure Monitor Storage Graphs Continue to Show High Storage Consumption

PostgreSQL uses Multi-Version Concurrency Control, commonly called MVCC. MVCC allows readers and writers to work at the same time without blocking each other unnecessarily. This is great for concurrency, but it changes how storage behaves after DELETE and UPDATE operations.

When you execute DELETE:

 
DELETE FROM orders
WHERE order_date < '2026-08-20';

PostgreSQL does not physically remove the rows from disk immediately.

Instead, it:

  1. Marks the rows as dead tuples.
  2. Keeps the row versions on disk so that transactions that started earlier can still see a consistent view of the data.
  3. Leaves the space occupied by those rows in the table file.

As a result:

  • Logical row count decreases.
  • Physical table size typically remains unchanged.
  • Azure Monitor storage graphs continue to show similar storage consumption.

The deleted pages remain allocated until PostgreSQL rewrites or reorganizes them.

When you execute UPDATE:

UPDATE customers 
SET city = 'London' 
WHERE customer_id = 100; 

PostgreSQL: 

  • Marks the existing row version as obsolete. 
  • Creates a new row version with the updated value. 
  • Retains the old version until no active transaction needs it. 

This behavior is a direct result of PostgreSQL's MVCC (Multi-Version Concurrency Control) architecture, which allows concurrent transactions without blocking readers.

This allows readers and writers to work concurrently, reduces locking, and gives long-running queries a consistent view of the data. The trade-off is that old row versions continue to occupy space until they can be cleaned up. 

Learn more about MVCC  PostgreSQL: Documentation: 7.1: Multi-Version Concurrency Control

 

Key Azure Metrics used for Storage analysis for Azure Database for PostgreSQL Flexible server:

The following metrics are usually the most useful during storage analysis:

Please refer Monitor Using Metrics and Logs in Azure Database for PostgreSQL Flexible Server - Azure Database for PostgreSQL | Microsoft Learn

After large DELETE operations, it's important to understand the difference between reclaiming database space for reuse and reducing Azure storage allocation and billing.

 Storage That Can Be Reclaimed

The following types of space can often be reclaimed or reused:

Dead Tuple Space

Obsolete row versions created by UPDATE and DELETE are called dead tuples. 

Dead tuples are no longer visible to normal queries, but they remain in the table files until vacuuming makes their space reusable. Workloads with frequent updates, deletes, batch processing, or ETL operations can accumulate large numbers of dead tuples. Autovacuum or manual VACUUM operations can clean up these tuples and make the space available for future database activity

How to Diagnose Table Bloat 

Start by checking tables with high dead tuple counts: 

SELECT 
    relname, 
    n_live_tup, 
    n_dead_tup 
FROM pg_stat_user_tables 
ORDER BY n_dead_tup DESC;

Below query identifies the largest tables: 

SELECT 
    relname, 
    pg_size_pretty(pg_total_relation_size(relid)) AS total_size 
FROM pg_catalog.pg_statio_user_tables 
ORDER BY pg_total_relation_size(relid) DESC; 

Tables that are both large and have high dead tuple counts are strong candidates for maintenance review. 

Table and Index Bloat

Over time, frequent INSERT, UPDATE, and DELETE operations can create bloat. This can be addressed using:

  • VACUUM FULL
  • pg_repack (preferred for production environments due to minimal locking)
  • Partition maintenance strategies

These operations can physically shrink tables and indexes, returning unused space to the operating system.

Temporary Storage Consumption

Storage consumed by temporary files generated from sorts, hash joins, and large query operations can be reclaimed automatically after the query completes. Identifying and tuning such workloads can prevent recurring storage spikes.

What Autovacuum Actually Does
PostgreSQL uses autovacuum to clean up obsolete row versions in the background. It also updates planner statistics and helps prevent transaction ID wraparound. 
A regular vacuum operation: 
  •     Removes dead tuples when they are no longer visible to any transaction. 
  •     Marks their space as reusable. 
  •     Updates statistics when configured and also the maintenance information. 
  •     Does not normally shrink the table file. 
Check whether Autovacuum is enabled: 
SHOW Autovacuum;

Below SQL query reviews tables with high dead-tuple counts:

SELECT 
    schemaname, 
    relname, 
    n_live_tup, 
    n_dead_tup, 
    last_autovacuum, 
    last_vacuum 
FROM pg_stat_user_tables 
ORDER BY n_dead_tup DESC; 
A high count may mean that autovacuum is not keeping up, large batch operations are producing dead tuples quickly, or old transactions are preventing cleanup.
Learn more about Autovacuum PostgreSQL: Documentation: 18: 19.10. Vacuuming 
 Following options helps us to reclaim/reuse the storage
Option 1: Standard VACUUM
VACUUM audit_logs;

Use standard VACUUM as routine maintenance. It makes deleted space reusable and helps improve database health, but it does not physically reduce table files.

Option 2: VACUUM FULL
To return unused table space to the operating system, PostgreSQL provides: 
VACUUM FULL schema_name.table_name; 

 VACUUM FULL rewrites the table into a compact physical file containing only the active rows. It can substantially reduce the table's file size. However, it has several operational costs: 

  •   It takes an ACCESS EXCLUSIVE lock. 
  •   Reads and writes to the table are blocked. 
  •   It may run for a long time on large tables. 
  •    It requires additional temporary storage during the rewrite. 
  •    Associated indexes may also need time and space to be rebuilt. 
 Please refer VACUUM FULL - PostgreSQL wiki 
Option 3: pg_repack

For production tables that cannot be unavailable for an extended period, pg_repack is often a better option. 

Example: 

pg_repack -d mydb -t schema_name.large_table 

It rebuilds tables and indexes with much less blocking than VACUUM FULL. A short lock may still be required during setup or the final object swap, so it is not entirely lock-free. 

Before using pg_repack on Azure Database for PostgreSQL, confirm that the required extension is supported and enabled for the server configuration. 

Please refer https://learn.microsoft.com/azure/postgresql/troubleshoot/how-to-perform-fullvacuum-pg-repack

Option 4: Partitioning for Future Prevention
For historical data such as audit logs, telemetry, or event tables, partitioning is often the best long-term solution. Instead of deleting millions of old rows, you can remove an old partition. 
DROP TABLE audit_logs_2026; 
This approach is faster, produces less bloat, and reduces the pressure on Autovacuum.
Azure storage metrics may remain high after rows are deleted and vacuumed. 
For example: 
This can happen for two separate reasons: 
  •  PostgreSQL may have converted the deleted space into reusable space without shrinking its files. 
  •  Azure may retain the server's provisioned storage allocation even after PostgreSQL files are compacted. 
As a result, a reduction in table size does not necessarily reduce the storage provisioned for the Flexible Server. Once Azure storage has grown, it generally should not be assumed that it will automatically shrink.

Provisioned Storage Cannot Be Reduced

Azure Database for PostgreSQL Flexible Server allows storage to be increased, but currently does not support reducing the configured storage size afterward.

For example:

Even if PostgreSQL reclaims internal space through VACUUM FULL or pg_repack, the server remains provisioned at 512 GB.

Storage Costs Continue Based on Provisioned Capacity

Because billing is tied to the provisioned storage allocation, deleting data does not automatically lower storage costs.

The following actions do not reduce provisioned storage billing:

  • DELETE operations
  • VACUUM
  • VACUUM FULL
  • Autovacuum cleanup
  • pg_repack

Best Practices to Avoid Storage Surprises

  • Do not run massive DELETE operations during peak workload hours.
  • Delete in controlled batches when a large purge is unavoidable.
  • Monitor dead tuples, table size, index size, WAL generation, and storage percent.
  • Investigate long-running transactions before assuming Autovacuum is broken.
  • Use partitioning for retention-based cleanup.
  • Use pg_repack or planned VACUUM FULL when physical compaction is required.
  • Set Azure Monitor alerts for storage utilization and plan capacity before full-disk scenarios occur.

Conclusion

When Azure PostgreSQL storage stays high after deleting, the database is usually behaving as designed. DELETE creates dead tuples and reusable internal space, but it does not automatically shrink physical files or reduce Azure allocated storage.

The right strategy depends on your goal. Use VACUUM for routine health, pg_repack for minimal-downtime compaction, VACUUM FULL when a maintenance window is acceptable, and partitioning to avoid large deletes in the first place.
Storage charges are tied to the amount of storage provisioned for the server. As a result, deleting data alone does not lower storage-related costs.

Published Aug 26, 2026
Version 1.0
No CommentsBe the first to comment