src/java/org/apache/cassandra/db/ColumnFamilyStore.java
/**
     * Truncate practically deletes the entire column family's data
     * @return a Future to the delete operation. Call the future's get() to make
     * sure the column family has been deleted
     */
Truncate practically deletes the entire column family's data
@return a Future to the delete operation. Call the future's get() to make
sure the column family has been deleted
Truncate practically deletes the entire column family ' s data @return a Future to the delete operation . Call the future ' s get () to make sure the column family has been deleted
public Future<?> truncate() throws IOException, ExecutionException, InterruptedException
    {
         Bonus complication: since we store replay position in sstable metadata,
         truncating those sstables means we will replay any CL segments from the
         beginning if we restart before they are discarded for normal reasons
         post-truncate.  So we need to (a) force a new segment so the currently
         active one can be discarded, and (b) flush *all* CFs so that unflushed
         data in others don't keep any pre-truncate CL segments alive.
         Bonus bonus: simply forceFlush of all the CF is not enough, because if
         for a given column family the memtable is clean, forceFlush will return
         immediately, even though there could be a memtable being flushed at the same
         time.  So to guarantee that all segments can be cleaned out, we need to
         "waitForActiveFlushes" after the new segment has been created.
        logger.debug("truncating {}", columnFamily);
         flush the CF being truncated before forcing the new segment
        forceBlockingFlush();
        CommitLog.instance.forceNewSegment();
        ReplayPosition position = CommitLog.instance.getContext();
         now flush everyone else.  re-flushing ourselves is not necessary, but harmless
        for (ColumnFamilyStore cfs : ColumnFamilyStore.all())
            cfs.forceFlush();
        waitForActiveFlushes();
         if everything was clean, flush won't have called discard
        CommitLog.instance.discardCompletedSegments(metadata.cfId, position);
         sleep a little to make sure that our truncatedAt comes after any sstable
         that was part of the flushed we forced; otherwise on a tie, it won't get deleted.
        try
        {
            Thread.sleep(100);
        }
        catch (InterruptedException e)
        {
            throw new AssertionError(e);
        }
        long truncatedAt = System.currentTimeMillis();
        snapshot(Table.getTimestampedSnapshotName(columnFamily));
        return CompactionManager.instance.submitTruncate(this, truncatedAt);
    }
public Future < ? > truncate ( ) throws IOException , ExecutionException , InterruptedException { logger . debug ( "truncating {}" , columnFamily ) ; forceBlockingFlush ( ) ; CommitLog . instance . forceNewSegment ( ) ; ReplayPosition position = CommitLog . instance . getContext ( ) ; for ( ColumnFamilyStore cfs : ColumnFamilyStore . all ( ) ) cfs . forceFlush ( ) ; waitForActiveFlushes ( ) ; CommitLog . instance . discardCompletedSegments ( metadata . cfId , position ) ; try { Thread . sleep ( 100 ) ; } catch ( InterruptedException e ) { throw new AssertionError ( e ) ; } long truncatedAt = System . currentTimeMillis ( ) ; snapshot ( Table . getTimestampedSnapshotName ( columnFamily ) ) ; return CompactionManager . instance . submitTruncate ( this , truncatedAt ) ; }
Truncate practically deletes the entire column family's data
 @return a Future to the delete operation.
Truncate practically deletes the entire column family ' s data @return a Future to the delete operation .
// We have two goals here: // - truncate should delete everything written before truncate was invoked // - but not delete anything that isn't part of the snapshot we create. // We accomplish this by first flushing manually, then snapshotting, and // recording the timestamp IN BETWEEN those actions. Any sstables created // with this timestamp or greater time, will not be marked for delete. // // Bonus complication: since we store replay position in sstable metadata, // truncating those sstables means we will replay any CL segments from the // beginning if we restart before they are discarded for normal reasons // post-truncate.  So we need to (a) force a new segment so the currently // active one can be discarded, and (b) flush *all* CFs so that unflushed // data in others don't keep any pre-truncate CL segments alive. // // Bonus bonus: simply forceFlush of all the CF is not enough, because if // for a given column family the memtable is clean, forceFlush will return // immediately, even though there could be a memtable being flushed at the same // time.  So to guarantee that all segments can be cleaned out, we need to // "waitForActiveFlushes" after the new segment has been created. // flush the CF being truncated before forcing the new segment // now flush everyone else.  re-flushing ourselves is not necessary, but harmless // if everything was clean, flush won't have called discard // sleep a little to make sure that our truncatedAt comes after any sstable // that was part of the flushed we forced; otherwise on a tie, it won't get deleted.