Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6880185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:56:32+00:00 2026-05-27T04:56:32+00:00

I need to delete about 2 million rows from my PG database. I have

  • 0

I need to delete about 2 million rows from my PG database. I have a list of IDs that I need to delete. However, any way I try to do this is taking days.

I tried putting them in a table and doing it in batches of 100. 4 days later, this is still running with only 297268 rows deleted. (I had to select 100 id’s from an ID table, delete where IN that list, delete from ids table the 100 I selected).

I tried:

DELETE FROM tbl WHERE id IN (select * from ids)

That’s taking forever, too. Hard to gauge how long, since I can’t see it’s progress till done, but the query was still running after 2 days.

Just kind of looking for the most effective way to delete from a table when I know the specific ID’s to delete, and there are millions of IDs.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:56:33+00:00Added an answer on May 27, 2026 at 4:56 am

    It all depends …

    • Assuming no concurrent write access to involved tables or you may have to lock tables exclusively or this route may not be for you at all.

    • Delete all indexes (possibly except the ones needed for the delete itself).
      Recreate them afterwards. That’s typically much faster than incremental updates to indexes.

    • Check if you have triggers that can safely be deleted / disabled temporarily.

    • Do foreign keys reference your table? Can they be deleted? Temporarily deleted?

    • Depending on your autovacuum settings it may help to run VACUUM ANALYZE before the operation.

    • Some of the points listed in the related chapter of the manual Populating a Database may also be of use, depending on your setup.

    • If you delete large portions of the table and the rest fits into RAM, the fastest and easiest way may be this:

    BEGIN; -- typically faster and safer wrapped in a single transaction
    
    SET LOCAL temp_buffers = '1000MB'; -- enough to hold the temp table
    
    CREATE TEMP TABLE tmp AS
    SELECT t.*
    FROM   tbl t
    LEFT   JOIN del_list d USING (id)
    WHERE  d.id IS NULL;      -- copy surviving rows into temporary table
    -- ORDER BY ?             -- optionally order favorably while being at it
    
    TRUNCATE tbl;             -- empty table - truncate is very fast for big tables
    
    INSERT INTO tbl
    TABLE tmp;        -- insert back surviving rows.
    
    COMMIT;
    

    This way you don’t have to recreate views, foreign keys or other depending objects. And you get a pristine (sorted) table without bloat.

    Read about the temp_buffers setting in the manual. This method is fast as long as the table fits into memory, or at least most of it. The transaction wrapper defends against losing data if your server crashes in the middle of this operation.

    Run VACUUM ANALYZE afterwards. Or (typically not necessary after going the TRUNCATE route) VACUUM FULL ANALYZE to bring it to minimum size (takes exclusive lock). For big tables consider the alternatives CLUSTER / pg_repack or similar:

    • Optimize Postgres query on timestamp range

    For small tables, a simple DELETE instead of TRUNCATE is often faster:

    DELETE FROM tbl t
    USING  del_list d
    WHERE  t.id = d.id;
    

    Read the Notes section for TRUNCATE in the manual. In particular (as Pedro also pointed out in his comment):

    TRUNCATE cannot be used on a table that has foreign-key references
    from other tables, unless all such tables are also truncated in the
    same command. […]

    And:

    TRUNCATE will not fire any ON DELETE triggers that might exist for
    the tables.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to delete many rows from sql server 2008 database, it must be
I have about 40 entities in Dynamics that I need to delete. Some of
I have a table about 10 million rows. We just imported it from another
I have a weekly script that moves data from our live database and puts
My issue at hand is that I need to remove about 60M records from
I have four DB tables in an Oracle database that need to be rewritten/refreshed
I have a database of around 20GB. I need to delete 5 tables &
I have a really large excel file and i need to delete about 20,000
I've got a table which has about 5.5 million records. I need to delete
I need to delete a temporary file from my C++ windows application (developed in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.